|
Practical Learning: Creating a Module
|
|
We use procedures and functions to create modular programs. Visual Basic statements are grouped in a block enclosed by
Sub , Function and matching End statements. The difference between the two is that functions return values, procedures do not.
A procedure and function is a piece of code in a larger program. They perform a specific task. The advantages of using procedures and functions are:
Procedures
A procedure is a block of Visual Basic statements inside
Sub , End Sub statements. Procedures do not return values.Option Strict On Module Example Sub Main() SimpleProcedure() End Sub Sub SimpleProcedure() Console.WriteLine("Simple procedure") End Sub End Module
This example shows basic usage of procedures. In our program, we have two procedures. The
Main() procedure and the user defined SimpleProcedure() . As we already know, the Main() procedure is the entry point of a Visual Basic program.SimpleProcedure()
Each procedure has a name. Inside the
Main() procedure, we call our user defined SimpleProcedure() procedure.Sub SimpleProcedure() Console.WriteLine("Simple procedure") End Sub
Procedures are defined outside the
Main() procedure. Procedure name follows the Sub statement. When we call a procedure inside the Visual Basic program, the control is given to that procedure. Statements inside the block of the procedure are executed.
Procedures can take optional parameters.
Option Strict On Module Example Sub Main() Dim x As Integer = 55 Dim y As Integer = 32 Addition(x, y) End Sub Sub Addition(ByVal k As Integer, ByVal l As Integer) Console.WriteLine(k+l) End Sub End Module
In the above example, we pass some values to the
Addition() procedure.Addition(x, y)
Here we call the
Addition() procedure and pass two parameters to it. These parameters are two Integer values.Sub Addition(ByVal k As Integer, ByVal l As Integer) Console.WriteLine(k+l) End Sub
We define a procedure signature. A procedure signature is a way of describing the parameters and parameter types with which a legal call to the function can be made. It contains the name of the procedure, its parameters and their type, and in case of functions also the return value. The
ByVal keyword specifies how we pass the values to the procedure. In our case, the procedure obtains two numerical values, 55 and 32. These numbers are added and the result is printed to the console.Functions
A function is a block of Visual Basic statements inside
Function , End Function statements. Functionsreturn values.
There are two basic types of functions. Built-in functions and user defined ones. The built-infunctions are part of the Visual Basic language. There are various mathematical, string or conversion functions.
Option Strict On Module Example Sub Main() Console.WriteLine(Math.Abs(-23)) Console.WriteLine(Math.Round(34.56)) Console.WriteLine("ZetCode has {0} characters", _ Len("ZetCode")) End Sub End Module
In the preceding example, we use two math functions and one string function. Built-in functionshelp programmers do some common tasks.
In the following example, we have a user defined function.
Option Strict On Module Example Dim x As Integer = 55 Dim y As Integer = 32 Dim result As Integer Sub Main() result = Addition(x, y) Console.WriteLine(Addition(x, y)) End Sub Function Addition(ByVal k As Integer, _ ByVal l As Integer) As Integer Return k+l End Function End Module
Two values are passed to the function. We add these two values and return the result to the
Main() function.result = Addition(x, y)
Addition function is called. The function returns a result and this result is assigned to the result variable.
Function Addition(ByVal k As Integer, _ ByVal l As Integer) As Integer Return k+l End Function
This is the Addition function signature and its body. It also includes a return data type, for the returned value. In our case is is an
Integer . Values are returned to the caller with the Return keyword. |