Subroutines and functions
Subroutines
A subroutine is a block of statements that carries out a well-defined task. The block of statements is placed within a set of “Sub..........End Sub” statements and can be invoked by name.
Example
Sub ShowDate()
MsgBox(now())
End Sub
The above subroutine displays the current date in the massage box and can be called by its name ShowDate().
The statements in a subroutine are executed, and when the End Sub statement is reached, control return to the calling program. It’s possible to exit a subroutine prematurely with the exit sub statement.
All variables declared with in a subroutine are local to those subroutines. When the subroutine exits, all the variables declared in it cease to exit.
Functions
A function is similar to subroutine, but a function return a result. Subroutines perform a task and don’t report anything to the calling program; functions commonly carry out calculation and report the result.
Because they return values, functions, like variable, have type (return data type). The value we pass back to calling program from a function is called return value, and its type must match the type of the function.
Function, accept arguments, just like subroutine. The statements that make up a function are placed in a set of “Function..........End Function” statement.
The result of a function is returned to the calling program with the return statement. The first time a return statement is executed. The function terminated and control is return to the calling program.
Example
Function NextDay() as Date
Nextday = date ADD(DateInterval.Day,1,Now())
End Function
Similar to variable, a custom function has a name, which must be unique in its scope. If we declare a function in a form, the function name must be unique in the form. If we declare a function as public or friend, its name must be unique in the project. Functions have the same scope rules as variables and can be prefixed by many of some keyboards. In effect, we can modify the default scope of a function with the keywords Public, Private, Protected, Friend and ProtectedFriend.
Arguments
Subroutines and Functions aren’t entirely isolated from the rest of the application. Must procedure accept arguments from the calling program. Recall that an argument is a value we pass to procedure and on which the procedure usually act. This is how subroutines and functions communicate with the rest of an application.
Functions also accept arguments, in many cases more than one.
Example
The function Min(), for instance, accepts two numbers and return the smaller are :-
Function Min(ByVal a As Integer, ByVal b As Integer) As Single
Min=IIF(a<b;a,b)
End Function
IFF() function is a built in function that evaluates the first argument, which is logically expression. If the expression is true, IFF() function return second argument; if the expression is False, IFF() return the third argument.
Argument passing Mechanisms
One of the most important issues is writing procedure is the mechanism use to pass arguments. The examples so far have used the default mechanism, passing arguments by value. The other mechanism is passing them by reference.
1. Passing argument by value:
When we pass an argument by value, the procedure sees only a copy of argument. The benefits of passing argument by value are isolated from the procedure, and only the code segment in which they are declared can change the values. This is the default argument passing mechanism in vb.net.
To specify the argument that will by pass by value, use the ByVal keyword in front of the argument name. If we omit the ByVal keyword, the editor will insert it automatically, since it is the default option.
Example
Function Degree(ByVal celcius As Single) As Single
Degree= (9/5)*celcius + 32
End Function
Note1: When we pass arguments to a procedure by reference, we’re actually passing the variable itself. Any changes made to the argument by the procedure we be permanent. When we pass arguments by value, the procedure gets a copy if the variable, which is discarded when the procedure ends. Any changes made to the argument by the procedure won’t affect the variable of the calling program.
Note2: When we pass an array as argument to a procedure, the array is always passed by reference even if we specify the ByVal keyword. The reason for this is that it would take a machine sometime to create a copy of array. Since a copy of array must also live in memory, passing too many arrays back and forth by value would depend our system’s memory.
2. Passing Argument by Reference
Passing argument by reference gives the procedure access to actual variable. The calling procedure passes the address of the variables in memory to that the procedure can change its value permanently. With VB6.0 is the Default argument passing mechanism.
Example
Function ADD(ByRef num1 as Integer,ByRef num2 as Integer) As Integer
ADD = num1 + num2
num1 = 0
num2 = 0
End Function
This function adds two numbers and then sets them to zero.