You are on page 1of 14

PARAMETE

RS
PASSING PARAMETERS
PARAMETER DEFINED
• A parameter is a value that is ‘passed’ (given) to a subroutine
(procedure or function).
• A parameter is a piece of data that is sent to a procedure when it is
called
• The data is used by the procedure to help it carry out its tasks
• The subroutine uses the value of the parameter within its execution.
The action of the subroutine will be different depending upon the
parameters that it is passed.
• Parameters are placed in parenthesis after the subroutine name. For
example Square(5) ‘passes the parameter 5 – returns 25
• The order and type of parameters that are passed to a function or
procedure must match the parameters in the function or procedure
header
PASSING PARAMETERS
• The formal parameter links the data in the calling
program (which will change) to its use in the function or
procedure.
• Actual parameter/argument is the data item supplied or
passed to the local variable
• Actual parameters can be passed to functions and
procedures either by value or by reference
IMPORTANT RULES FOR PASSING
PARAMETERS
• The number of actual and formal parameters must be the same
• Parameters are matched according to their position, not according to
their names
• The data types of a matching pair of parameters must be the same
• A reference parameter is used when a piece of data needs to be
passed out of the procedure
• A value parameter is a piece of data used by the procedure. Any
changes made to it inside the procedure are not passed back to
again.
PASSING BY VALUE
• Only the value of the parameter is passed to the
procedure called.
• Value of the parameter can be manipulated by the
procedure called.
• When the procedure called is terminated the new value is
discarded and the value of the parameter in the calling
procedure returns to the original value

EXAMPLE TO ILLUSTRATE PASSING A PARAMETER BY
VALUE

Procedure1
Declare a
a = 5
Call Procedure2 (a)
Display a
EndProcedure1
Procedure2 (ByVal b)
b = b + 2
EndProcedure2
• The output of the parameter will be the display of value 5
PASSING BY REFERENCE
• The parameter is stored in the original location and
only a pointer (a reference to the parameter’s memory
address) is passed to the procedure called.
• Any changes made to the reference passed to the
procedure called, will remove the value of the
parameter at the original location.
• When the procedure called is terminated the new value
is available to the calling procedure
EXAMPLE TO ILLUSTRATE PASSING A PARAMETER BY
REFERENCE

Procedure1
Declare a
a = 5
Call Procedure2 (a)
Display a
EndProcedure1
Procedure2 (ByRef b)
b = b + 2
EndProcedure2
• The output of the parameter will be the display of value 7
VB ILLUSTRATION: PASSING BY VALUE
Private Sub cmdByVal_Click()
Dim number As Integer
number = txtNumber.Text
Call Display1(number)
Print number
End Sub
Public Sub Display1(ByVal num As Integer)
num = num + 2
End Sub

VB ILLUSTRATION: PASSING BY REFERENCE
Private Sub cmdOk_Click()
Dim number As Integer
number = txtNumber.Text
Call Display(number)
Print number
End Sub

Public Sub Display(ByRef num As Integer)
num = num + 2
End Sub
WHAT IS THE DIFFERENCE
BETWEEN PASS BY VALUE AND
PASS BY REFERENCE?
PAIR WORK
Design a VB program which finds the smaller
number between two numbers. You are
suppose to use parameter passing in your
code.
Hint: if statement may be used in the public
sub procedure to find the smaller number
which must be passed to the procedure
calling it.
SUMMARY
• A parameter is a piece of data that is sent to a procedure
when it is called
• Actual parameters are passed to the procedure and are
matched to the formal parameters which are part of the
procedure’s declaration. The data type of a matching pair of
actual and formal parameters must be the same
• Formal parameters are divided into value and reference
parameters. Any changes made to a reference parameter
inside a procedure are passed out again. Changes to a value
parameter cannot be passed out again.
SUMMARY CONTINUED
• You do not have to declare formal parameters as value(ByVal)
or by reference (ByRef). If you don’t, Visual Basic defaults to
reference
• When you pass a parameter by reference when it should be
by value no errors occur, but if you pass by value when it
should have been by reference, a serious error will occur.