You are on page 1of 10

Chapter 6: VB.

NET functions & sub procedures dedicated/specific task and it’s upon the programmer to
decide when to break the program into these small
6.1 Chapter objectives
manageable modules.
By the end of this topic, learners should be able to:
Different programming languages refer to these modules
a) Demonstrate understanding what modular using different terms. In C and C++, we call this small
programming is module functions, In Java, C# we call them methods while
b) Explain the benefits associated with in Visual Basic.NET, we call them sub
modular programming procedures/functions. Ideally, each of the module is
c) Demonstrate understanding on how to define and supposed to perform a specific function hence avoiding
invoke methods in functions and sub procedures in code duplication, simplifying program maintenance and
VB.NET debugging as well as promoting program portability
d) Understand the difference between an argument
and parameters.
e) Exhibit the different parameter passing techniques In the previous sections, we have encountered a
6.2 Introduction programming approach where all the code (statements) has
been written inside a single block e.g. a certain event handler
Modular programming is an art of software engineering
block. In the next sections we will learn how to program in a
whereby programmers/software engineers break large
modular way by writing additional user defined functions.
and complex programs into manageable portions known
as modules. Each of the module will perform a
6.3 Benefits associated with modular programming multiple places in a program, we can simply create a
i. Less code duplication since each module will do a function and call it whenever required.
specific task.
6.5 Defining a Function in VB.NET
ii. It’s also easy to maintain the program since the
programmer is working with small program The syntax to define a function in VB.NET is as follows :

segments/module. [Access_specifier ] Function Function_Name [ (ParameterList) ]


iii. Simplifies debugging since each program logic can As Return_Type
be verified separately
[ Block of Statement ]
iv. Modules allow you to reusable the same code over
Return return_val
and again in different programs with less/little
code duplication End Function

6.4 VB.NET function Where,

In VB.NET, the function is a separate group of codes that are ▪ Access_Specifier: It defines the access level of the
used to perform a specific task when the defined function is function such as public, private, or friend, Protected
called/invoked in a program. It’s possible to create more function to access the method.
than one function in a program to perform various ▪ Function_Name: The function_name indicate the
functionalities. The function is also useful to code name of the function that should be unique.
reusability by reducing the duplicity of the code. For ▪ ParameterList: It defines the list of the parameters
example, if we need to use the same functionality at to send or retrieve data from a method.
▪ Return_Type: It defines the data type of the variable End Function
that returns by the function

The following are the various ways to define the function in


Example:
a VB.NET.
Write a program to find the sum and subtraction of two
Public Function example () As string
numbers using the function.
' Statement to be executed
We first define the two functions as shown below
End Function
Function Sum (ByVal x As Integer, ByVal y As Integer) As Integer
' Define the local variable.
Dim sum As Integer = 0
Private Function example(ByVal x As String) As double sum = x +y
Return sum
' Statement to be executed
End Function
End Function
Function Sub (ByVal x As Integer, ByVal y As Integer) As Integer
' Define the local variable.
Dim sub As Integer
Public Function example(ByVal username As x, ByVal y As
subtract =x - y
Integer) As integer Return sub
End Function
' Statement to be executed
6.6 Function call/invocation The body of the function is executed and returns the value of
sum; x+y. This value is passed back to the calling block. The
Function definition is completely nothing since by itself it
memory allocated to the parameters and local variables is
does nothing unless instructed to execute. For a function to
then destroyed to free memory. The value returned is
execute, it needs to be called/invoked. The general syntax
assigned to the variable on the left-hand side, result, in the
for calling a function is as follows
expression used to call the function.
Function_name (<argument list>)
The net effect of executing the function in our example is that
For example, to call a function named sum and pass to it an the variable result has been assigned the value of the sum of
argument called number, we can use the following VB.NET the two integers; x and y.
code
A function can be called any number of times from the same

Dim x As Integer = txtFirst.Text or different parts of the program. It can be called with
Dim y As Integer = txtSecond.Text different parameter values (though they must be of the
Result = Sum (x, y); correct type).
The above statement calls the function sum () and passes a
copy of the value stored in the variables x and y. When the The same syntax can be used to call the Sub() function

function is called/Invoked, computer memory is allocated 6.7 VB.NET Sub


for the parameters, x and y, and the value passed is copied to
A Sub procedure is a separate set of codes that are used in
these memories.
VB.NET programming to execute a specific task but as
opposed to a function, it doesn’t return any values. The Sub The following are the different ways to define the types of
procedure is enclosed by the Sub and End Sub statement. Sub method.

6.8 Defining the Sub procedure Public Sub Example()

To define a sub procedure in VB.NET, we use the following ' Statement to be executed
syntax of the Sub procedure:
End Sub
[Access_Specifier ] Sub Sub_name [ (parameterList) ]

[ Block of Statement to be executed ]


Private Sub Example( ByVal x As String) As String
End Sub
' Statement to be executed
Where,
End Sub
o Access_Specifier: It defines the access level of the
procedure such as public, private or friend, Protected.
Public Sub Example( ByRef x As String, ByRef y As Integer)
o Sub_name: The Sub_name indicates the name of the
Sub that should be unique. ' Statement to be executed

o ParameterList: It defines the list of the parameters End Sub

to send/retrieve data from a method.


Example: Write a simple program to pass the empty, a Sub Sum(ByVal x As Integer, ByVal y As Integer)
single or double parameter of Sub procedure in the VB.NET.
' Define the local variable.
Sub Welcome()
Dim sum As Integer = 0
MsgBox("Welcome")
sum = x+ y
End Sub
MsgBox(" Sum of two number is : {0}", sum)

End Sub

Sub circle(ByVal radius As Double)

Dim Area As Double


Sub SubT(ByVal x As Integer, ByVal y As Integer)
Const PI as Double = 3.14
' Define the local variable.
Area = PI * r * r
Dim sub As Integer
MsgBox(" Area Of circle is : {0}", Area)
sub = x - y
End Sub
MsgBox(" Subtraction of two number is : {0}", sub)

End Sub
Sub Mul(ByVal x As Integer, ByVal y As Integer) Dim x As Integer = txtFirst.Text
Dim y As Integer = txtSecond.Text
' Define the local variable.
Mul (x, y);
Dim mult As Integer The above statement calls the procedure Mul () and passes a
copy of the value stored in the variables x and y. When the
mult = n1 * n2
sub procedure is called/Invoked, computer memory is
MsgBox(" Multiplication of two number is : {0}", mult) allocated for the parameters, x and y, and the value passed is

End Sub copied to these memories.

6.9 Sub Procedure call/invocation


The body of the sub procedure is executed and returns the
Sub procedure definition is completely nothing since by
but doesn’t return a value back to the calling block. The
itself it does nothing unless instructed to execute. For a sub
memory allocated to the parameters and local variables is
procedure to execute, it needs to be called/invoked. The
then destroyed to free memory.
general syntax for calling a sub procedure is as follows
Just like a function, a sub procedure can be called any
sub_procedure_name (<argument list>)
number of times from the same or different parts of the
For example, to call a sub procedure named Mul and pass the program. It can be called with different parameter values
two arguments called x and y, we can use the following (though they must be of the correct type).
VB.NET code
The same syntax can be used to call the other sub procedures
is the same as shown below
parameter that do not affect the Sub procedure's formal
argument/parameter. The syntax is s follows
Welcome() ' call welcome() procedure
Sub Sub_method( ByVal parameter_name As datatype )
Sum (x,y) 'call Sum() procedure
[ Statement to be executed]
subtT (x,y) 'call subT() procedure
End Sub
circle(7)’calls the circle procedure
Example, let’s create a program to understand the concept
of passing parameter by value.

Dim num1, num2 As Integer


6.10 parameter passing mechanism
Num1=txtNumber1.Text
In the VB.NET programming language, we can pass
Num2=txtNumber2.Text
parameters in two different ways; pass by Value or pass by
reference LblNumber.Text=”Before swapping the value of 'num1' is {
0}", num
5.8.1 Passing parameter by Value
lblNumber2.Text=”" Before swapping the value of 'num2' i
It’s the default in VB.NET. we call a method , copies of the
s {0}", num2
actual value of an argument are passed for each parameter.
Therefore, any changes made to the main function's actual call swap function
swap(num1, num2) Using this mechanism, we pass the reference (memory
address) of an argument in the calling function to the
LblNumber.Text=”Before swapping the value of 'num1' is {
corresponding formal parameter of the called function.
0}", num
Therefore, called function can modify the value of the
lblNumber2.Text=”" Before swapping the value of 'num2' is argument by using its reference passed in. The parameter
{0}", num2 and arguments share the same memory location as the
' swap () method actual parameters supplied to the method.

Sub swap(ByVal a As Integer, ByVal b As Integer) The syntax for the passing parameter by Reference:

' Declare a temp variable Sub Sub_method( ByRef parameter_name1, ByRef Parameter_na
me2 )
Dim temp As Integer
[ Statement to be executed]
temp = a ' save the value of a to temp
End Sub
b = a ' put the value of b into a
In the above syntax, the ByRef keyword is used to pass the
b = temp ' put the value of temp into b
Sub procedure's reference parameters.
End Sub
Let's create a program to swap the values of two variables
5.8.2 Passing parameter by Reference using the ByRef keyword. We use the above example to
demonstrate the concept of calling by reference
Dim num1, num2 As Integer Dim temp As Integer

Num1=txtNumber1.Text temp = a ' save the value of a to temp

Num2=txtNumber2.Text b = a ' put the value of b into a

LblNumber.Text=”Before swapping the value of 'num1' is { b = temp ' put the value of temp into b
0}", num
End Sub
lblNumber2.Text=”" Before swapping the value of 'num2' is
{0}", num2

call swap function

swap(num1, num2)

LblNumber.Text=”Before swapping the value of 'num1' is {


0}", num

lblNumber2.Text=”" Before swapping the value of 'num2' is


{0}", num2

' swap () method

Sub swap(ByRef a As Integer, ByRef b As Integer)

' Declare a temp variable

You might also like