Matakuliah
Tahun
Versi
: D0524 / Algoritma dan Pemrograman Komputer
: 2005
:
Pertemuan 07
Procedures
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Menerapkan penggunaan prosedur
2
Outline Materi
• Procedures
• Needed for Procedures
• Parameter Passing
3
Procedures
• In program design, we break the overall task into sub tasks.
These subtasks are implemented using procedures. Each
procedure should carry out one sub task.
• A procedure is simply a way of performing code 'out of line'.
When a program encounters a call to a procedure, it suspends
its current path through the code statements and passes control
to a separate chuck of code. When this separate chuck has
been executed, control returns to the statement immediately
following the call:
Private Sub cmdButton_Click()
…
Private Sub aProcedure
Call aProcedure
…
…
End Sub
End Sub
4
Procedures
• All procedures consist of a header that indicates the
procedure name and any information that it requires
• Body that performs the task
• If a procedure is to be used by a number of different
forms it should be declared as Public within the code
module otherwise it should be declared as Private in the
general declaration section of the form.
5
Needed for Procedures
• A simplification of the program structure
• Reduction in the amount of code that is
repeated
• Ability to focus on what needs to be done
rather than how it is to be achieved
6
Parameter Passing
• Procedures are not very useful though unless
they can communicate data
• Data passed between procedures are called
parameters
• Parameters can be sent to the procedure for use
within it, e.g. calculations
• Results from the procedure can be sent back by
the procedure to the calling program
7
Parameter Passing - Example
Main
keyInput num1
keyInput num2
IF num1 < num2 THEN
Call Swap(num1, num2)
ENDIF
conOutput “Largest”, num1
Swap (num1, num2)
temp = num1
num1 = num2
num2 = temp
The variables num1 and num2 are passed to the
procedure Swap for exchanging. After the procedure,
the largest number is in num1.
8
Parameter Passing – VB Solution
Private Sub Form_Click()
num1 = InputBox("enter number 1")
num2 = InputBox("Enter number 2")
If num1 < num2 Then
Call Swap(num1, num2) ‘call to the procedure
End If
MsgBox "Largest " & num1
Parameter list for procedure
End Sub
Private Sub Swap(num1 As Integer, num2 As Integer)
Dim temp As Integer ‘temp is a var. local to Swap
temp = num1
num1 = num2
num2 = temp
End Sub
9
Parameter Passing - Rules
• Values are copied into the parameters The order
of parameters declared in the procedure MUST
match the order in which they were called
• The data types must match. E.g. 1 is OK, e.g. 2
is not and will crash program!
E.g. 1 Call Swap(num1, num2)
Private Sub Swap(num1 As Integer, num2 As Integer)
E.g. 2 Call AnyProc(myName, myAge)
Private Sub AnyProc(myAgeAs Integer, myName As String)
10
© Copyright 2026 Paperzz