JK VB NET 7 Procedures and Functions
JK VB NET 7 Procedures and Functions
Slide 6- 1
Procedures and Functions
Revision Question
(i) Write a function procedure that computes the
perimeter of a rectangle.
(ii) Write a procedure that makes use of the
function above to calculate the perimeter of a
rectangle.
Slide 6- 3
Introduction
A procedure is a collection of statements that
performs a task
Event handlers are a type of procedure
Inputbox()
A method can be either a procedure or a
function
Procedures
1
You Can Write Your Own General Purpose
Procedures That Perform Specific Tasks
General Purpose Procedures Are Not Triggered
by Events but Called From Statements in Other
Procedures
Procedures
A moderately complex program needs to perform multiple tasks.
worker4 worker5
Sub Procedures vs. Function
Procedures
Friend
Protected Friend
Slide 6- 13
Private sub arearec (ByVal L as integer, ByVal W as
integer )
dim area as integer
L=10
W= 5
area = L*W
Return….
End sub
Slide 6- 14
Public Class Form1
Private Sub bntSub_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles bntSub.Click
addNumbers()
MsgBox("Excution back to the bntSub _click")
End Sub
Returns to Calls addNumbers
btnSub_Click Private Sub addNumbers() procedure
Dim num1 As Integer = 23354
Dim num2 As Integer = 236328
MsgBox(num1 + num2)
End Sub
End Class
Slide 6- 16
Passing Arguments to a
2 Procedure
areaRec(10,5)
Slide 6- 24
Passing Arguments ByVal or ByRef
Arguments are usually passed ByVal
New storage location created for procedure
storage location
Any changes are made to the original value
End Function
End Class
Slide 6- 29
Summary
Procedures
The declaration for a procedure begins with a Sub statement and ends
with an End Sub statement. The code that appears between these two
statements is the body of the procedure.
When a procedure call executes, the application branches to that
procedure and executes its statements. When the procedure has finished,
the application branches
has access to the original argument and can make changes to it.
Functions
• A function returns a value to the part of the program that called it.
End Sub
End Class
Public Class Form1
End Sub
Public Class Form1
End Sub
End Sub
End Class