Docsity

Parameter Passing in Visual Basic: By Reference and By Value, Study notes of Computer Science

Toggle sidebar
bg1
Parameter Passing
CS 1103
The names in parentheses in a Private Sub or Private Function statement are called
parameters. For example, x and y are parameters in the statement
Private Sub PP1(x As Integer, y As Integer)
The items in parentheses in a Call statement or in a function invocation statement
(cc = Func1 below) are called arguments. Arguments may be variables, literal constants,
named constants, or expressions. In the two statements just below, aa, 3.7, bb-2 and
“Hello” are arguments. aa is a variable, 3.7 and “Hello” are literal constants, and bb-2 is
an expression.
Call PP2(aa, 3.7)
cc = Func1(bb - 2, aa, ”Hello”)
Parameters and arguments are matched by position, not by name:
first parameter <-------> first argument
second parameter <-------> second argument
etc.
The name of an argument variable may be the same as the name of the corresponding
parameter, but it doesn’t have to be. They must have the same type, however, with only
one exception, to be covered below.
If an argument is a variable, it is usually passed (see below) to the subprogram “by
reference” (or, equivalently, “by address”). No new copy of the variable is made for use
in the subprogram; the subprogram “is told” where to find the argument variable. If the
value of the parameter is changed, the value of the corresponding argument variable is
changed immediately. Therefore an argument variable passed by reference can be used
to send information into a subprogram or to receive information back from the
subprogram, or both. The purpose of “call by reference” is to allow a called subprogram
to send information back to the calling subprogram.
Under certain circumstances an argument is passed to a subprogram “by value”. This
happens if
1) the argument is a literal constant, a named constant, or an expression; or
2) the argument is a variable enclosed in an extra set of parentheses (see
Comment 5 on page 129); or
3) the corresponding parameter is declared to be passed ByVal:
pf2

Partial preview of the text

Download Parameter Passing in Visual Basic: By Reference and By Value and more Study notes Computer Science in PDF only on Docsity!

Parameter Passing

CS 1103

The names in parentheses in a Private Sub or Private Function statement are called parameters. For example, x and y are parameters in the statement Private Sub PP1(x As Integer, y As Integer) The items in parentheses in a Call statement or in a function invocation statement (cc = Func1 below) are called arguments. Arguments may be variables, literal constants, named constants, or expressions. In the two statements just below, aa, 3.7, bb-2 and “Hello” are arguments. aa is a variable, 3.7 and “Hello” are literal constants, and bb-2 is an expression. Call PP2(aa, 3.7) cc = Func1(bb - 2, aa, ”Hello”) Parameters and arguments are matched by position, not by name: first parameter <-------> first argument second parameter <-------> second argument etc. The name of an argument variable may be the same as the name of the corresponding parameter, but it doesn’t have to be. They must have the same type, however, with only one exception, to be covered below. If an argument is a variable, it is usually passed (see below) to the subprogram “by reference” (or, equivalently, “by address”). No new copy of the variable is made for use in the subprogram; the subprogram “is told” where to find the argument variable. If the value of the parameter is changed, the value of the corresponding argument variable is changed immediately. Therefore an argument variable passed by reference can be used to send information into a subprogram or to receive information back from the subprogram, or both. The purpose of “call by reference” is to allow a called subprogram to send information back to the calling subprogram. Under certain circumstances an argument is passed to a subprogram “by value”. This happens if

  1. the argument is a literal constant, a named constant, or an expression; or
  2. the argument is a variable enclosed in an extra set of parentheses (see Comment 5 on page 129); or
  3. the corresponding parameter is declared to be passed ByVal:

Private Sub PP3(uu As Integer, ByVal ww As String) When an argument is passed by value, a new “local” copy of this argument is created in the called subprogram, at the time the Call statement is executed. The corresponding parameter can be used, tested, or changed, but no new information is ever passed back to the calling subprogram through this argument. When the program returns from the called subprogram, the local copy is destroyed. Value arguments are used to carry information into a subprogram, only. The purpose of “call by value” is to protect an argument from being changed when the programmer does not want it to be changed. Variables and/or named constants can be declared at “form level”, before the first Private Sub or Private Function. (We have already done this with named constants; the author does it with array variables in chapter 4.) “Form level” is also called “global”. The names of form-level quantities can be declared locally, in a Private subprogram. If this is done, the form-level quantities are ignored and the local name declared in the local Dim is used, inside that subprogram. ‘ Declare a form level named constant, maxVal. ‘ (This might normally be spelled MAX_VAL.) Const maxVal As Integer = 20 Private Sub PP4() picBox.Print maxVal ‘ The value of the form level named constant, 20, ‘ was printed here. End Sub Private Sub PP5() Dim maxVal As Integer ‘ In this subprogram, maxVal is a local variable, ‘ not the form level Const. maxVal = 50 picBox.Print maxVal End Sub The general “scope rule” is that names declared (using Dim or Const) in a Private subprogram cannot be “seen” from outside of that subprogram (for example, from within another Private subprogram), but form-level names can be seen from inside of a Private subprogram if those names were not declared (as parameters, named constants, or variables) in that subprogram. Whenever a name is used in a Private subprogram, Visual Basic first looks to see if that name was declared inside that subprogram using Const or Dim. If it was, then that declaration is used for that name. If it wasn’t, then VB looks for the name at form level. The rule followed by VB is: “Look in this subprogram first. If not found here, look outside.” 24 September 2003