title=Fundamentals of Programming: Functions and Procedures
To save you rewriting lots of code again and again you might use a sub routine, there are two types: Procedures and Functions. For example in a program you wanted to know today's date, instead of having to write a separate sub routine to calculate the date each time you wanted to work it out, you would probably use
date(). This is a function, when you call it, it returns a value. It was written by someone else and you can keep reusing it as many times as you want. Any program written in industry will use sub routines calling things like:
console.writeline(), printScore(), deleteRecord() . Procedures and Functions allow for you to:
- Reuse code
- structure your programming
- Easily incorporate other peoples code
An easy way to tell the difference between a Procedure and a Function is to look at the names:
- Functions are fun: if you would call them, they would return a value
- Procedures aren't fun: if you call them they don't return any value. (these are known as
subin Visual Basic)
| In VB.NET you declare a procedure by using the
Declarations are where you state the name of your procedure/function and the code that you want to execute. Even if you declare a procedure/function, it doesn't mean that the code will run, you need a Call to actually get the code to execute.
Sub printNumber() console.writeline(number1) End Sub
Functions are slightly different, as they return values you must include a return function in their declaration. And you must specify the datatype of the value being returned, in the case below that is an Inteteger specified by:
..) as Integer
Function printNumber() as Integer return number1 End Function
Calls allow you to run the code declared in a procedure/function. You can build up all sorts of programming structures by making Calls part of the code. Remember that Functions are fun, so you should be doing something with the returned value.
printNumber() ' a procedure call
console.writeline(printNumber()) ' a function call dim x = MaximumSpeed() ' another function call
Parameters allow you to pass values to the procedures and functions that you declare, you can pass all sorts of datatypes as parameters and you can pass as many as you like
'declaration Sub printNumber(number1 as integer) 'one parameter console.writeline(number1) End Sub '... 'call printNumber(4)
The output would be:
'declaration Sub printNameAge(name as string, age as integer) 'two parameters console.writeline(name & " is " & age & " years old") End Sub '... 'call printNameAge("Mounir", 17)
The output would be:
'declaration function squareNumber(number1 as integer) as integer 'one parameter return (number1 * number1) End Function '... 'call console.writeline(squareNumber(4))
Note that as it's a function, we had to include the call in an equation. It returns a value, it can't sit on its own. The output would be:
Functions return values, Procedures don't
Function Avg(num1, num2, num3) return (num1 + num2 + num3) / 3 End Function
dim a, b, c as integer console.write("input num1 = ") a = console.readline() console.write("input num2 = ") b = console.readline() console.write("input num3 = ") c = console.readline() console.write("Average = " & Avg(a,b,c))
The parameter that you are passing to a procedure or function is referred to. That means you are pointing at it, you are going to directly change its value and anything that happens to it within the procedure or function will change the original value.
Dim number1 as integer = 123 Sub Main() console.writeline(number1) IncPrintNumber(number1) console.writeline(number1) End Sub Sub IncPrintNumber(ByRef num as integer) num = num + 1 console.writeline(num) End Sub
The output would be:
The parameter that you are passing to a procedure or function is copied. That means you are taking a copy of the original value put into the procedure or function call. Anything that happens to it within the procedure or function will NOT change the original value.
Dim number1 as integer = 123 Sub Main() console.writeline(number1) IncPrintNumber(number1) console.writeline(number1) End Sub Sub IncPrintNumber(ByVal num as integer) num = num + 1 console.writeline(num) End Sub
This saves a local variable of the number1 value, storing it in num, it is only valid inside the IncPrintNumber sub routine The output would be:
A parameter passed by value copies the value of the parameter passed into the sub routine. Any changes made to this value do not impact on the original value.
A parameter passed by Reference passes a link to a variable. Any changes made to the parameter in the sub routine change the original variable.