

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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
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