You are on page 1of 10

6

Upon completion of this chapter , you should be able to:

1. Explain the difference between Sub Procedures and Function


Procedures
2. Implement Sub Procedures and Function Procedures.
3. Pass information to a procedure
4. Explain the difference between passing data by value and
passing data by reference
Chapter 6 Applying Procedures and Functions

6.1 Function Procedures and Sub Procedures

PROCEDURES:
A block of program code that performs a
specific task

Function procedure:
Sub procedure:
Returns a value after performing
its task Does not return a value

Two types of
Sub
procedures: Independent
Event Sub procedure:
procedure:

Collection of code
Sub procedure that that can be invoked
is associated with a from one or more
specific object and places in an
event application

Automatically
processed when the Not associated with
associated event an event
occurs

Processed only when


called (invoked)

Figure 6.1 Function Procedures and Sub Procedures

Page 68
Visual Basic .Net Programming

6.2 Function Procedures

6.2.1 How to Create A Function Procedures

Function function_name (parameter_list) As Datatype

Statements

Return expression

End Function

Figure 6.2 Create a Function Procedures Syntax

6.2.2 How To Call Function Procedure

variable = function_name()

Figure 6.3 Call Function Procedure Syntax

6.2.3 Implement Function Procedures

Create an application to calculate an area of a circle as shown in Figure 6.4.


The application uses function to perform the calculations and the code as
shown in Figure 6.5.

Page 69
Chapter 6 Applying Procedures and Functions

Figure 6.4 Function Interface

Function GetCircleArea(ByVal rad As Integer) As Double

Dim luas As Double

luas = 3.142 * rad * rad

Return luas

End Function

Private Sub btncal_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles btncal.Click

Dim radius As Integer

Dim kira As Double

radius = txtradius.Text

kira = GetCircleArea(radius)

lblarea.Text = kira.ToString("n2")

End Sub

Figure 6.5 Function Code

Page 70
Visual Basic .Net Programming

6.3 Independent Sub Procedures

6.3.1 How to Create an Independent Sub Procedure

Sub procedure_name()

Statements

End Sub

Figure 6.6 Create an Independent Sub Procedure Syntax

6.3.2 How to Call an Independent Sub Procedure

Call procedure_name()

OR

procedure_name()

Figure 6.7 Call an Independent Sub Procedure Syntax

<< INFO >>

 Call keyword is optional.

Page 71
Chapter 6 Applying Procedures and Functions

6.3.3 Implement Independent Sub Procedure

Add listbox and button to your form. The output as shown in Figure
6.10.

Private Sub btngo_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btngo.Click

lstoutput.Items.Add("Hello from GO button")

lstoutput.Items.Add("Now I am calling
Write in
DisplayMessage procedure")
one line
DisplayMessage()

lstoutput.Items.Add("Now I am back in GO button")

End Sub

Figure 6.8 Event Procedure

Sub DisplayMessage()

lstoutput.Items.Add("")

lstoutput.Items.Add("Hi from DisplayMessage procedure")

lstoutput.Items.Add("")

End Sub

Figure 6.9 Independent Sub Procedure

Page 72
Visual Basic .Net Programming

Figure 6.10 Output Interface

6.4 Passing Variables

Variable can be passed


to a procedure in two
Each variable has a ways:
value and a unique
memory address •By value: you pass the
variable’s value
•By reference: you pass
the variable’s address

Passing by value: Passing by reference:


The procedure receives The procedure receives
only the value and the address and can
cannot change the actual make changes to the
variable’s value variable’s value

Figure 6.11 Passing Variables

Page 73
Chapter 6 Applying Procedures and Functions

6.4.1 Implement Passing Argument By Val and By Ref

Add listbox and button to your form. The output as shown in Figure
6.14.

Private Sub btnval_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnview.Click

Dim num As Integer = 100

lstval.Items.Add("Inside View Button.

The value of num is " & num)

Change(num)

lstval.Items.Add("Now back in View Button. The value

of num is " & num)

End Sub

Sub Change(ByVal nombor As Integer)

lstval.Items.Add("Inside Change Procedure.

The value of num is " & nombor)

nombor = 5

lstval.Items.Add("The number is now " & nombor)

End Sub

Figure 6.12 Passing Argument By Val

Page 74
Visual Basic .Net Programming

Private Sub btnref_Click_1(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles btnref.Click

Dim num As Integer = 100

lstref.Items.Add("Inside View Button.

The value of num is " & num) Write in


one line
Tukar(num)

lstref.Items.Add("Now back in View Button.

The value of num is " & num)

End Sub

Sub Tukar(ByRef nombor As Integer)

lstref.Items.Add("Inside Tukar Procedure.

The value of num is " & nombor) Write in


one line
nombor = 5

lstref.Items.Add("The number is now " & nombor)

End Sub

Figure 6.13 Passing Argument By Ref

Page 75
Chapter 6 Applying Procedures and Functions

Figure 6.14 Output

Page 76

You might also like