This is always a hot topic at this time of year. It’s been a while since we did procedures, parameters, byVal, Byref & Data Flow
From Shawlands Acc
- Data flow within a program is handled by parameters
- A parameter is a value or a variable which is passed into or out from a subroutine
- The parameters used in a procedure call are ACTUAL parameters
- The parameters used in a procedure definition are called FORMAL parameters
- A parameter is passed BY REFERENCE if it is to by passed into and back out of a procedure
- A parameter is passed BY VALUE if it is to by passed into but NOT back out of a procedure
- Arrays are ALWAYS passed by reference
- The use of parameters means that variables can be kept local to subroutines
- This leads to programs being more reliable and robust
Data in/out in programs
Consider the following example.
Write a program using procedures that gets 2 numbers, adds them together before displaying the answer.
I have had a lot of questions about ByVal and ByRef so I thought I would clear it up in a quick posting. Continue reading Data Flow Revisited
We started of with SQA 2008 Q 1,3,&4 for a bit of revision.
Working with the program from last week we explored the concept and implementation of procedures (subprograms).
A procedure is a sub program with it own variables and routines, it is called from the main program with a procedural call and created by a procedural
A local variable is used within subprograms and is not passed back to the main program, this makes its domain the sub program.
A global variable is used when a piece of data needs to be seen in all sub programs, its domain is then the whole program. This is very bad programming form and should not be used.
Parameter passing allows the programmer to pass variables both in to and out of a sub program.
- By value (ByVal) is used for input when the programmer does not want the subprogram to change the contents of the variable. The name comes from VB passing only the value of the variable.
- By reference (ByRef) is used for output when the programmer wants to get data from the subprogram. The name comes from VB passing the memory reference of the variable and therefor allows the subprogram to directly edit it.
The picture above is the final program we created you can find the code Here
Description and exemplification of
- parameter passing (in, out, in/out)
- call by reference/value
Continue reading FAQ By Value or By Reference
Private Sub Command1_Click()
‘Set up variables
Dim NumA As Single
Dim NumB As Single
Call Get2Numbers(NumA, NumB)
Call DisplaySum(NumA, NumB)
Sub Get2Numbers(ByRef NumA, ByRef NumB)
NumA = InputBox(“What is the first number”)
NumB = InputBox(“What is the second number”)
Sub DisplaySum(ByVal NumA, ByVal NumB)
MsgBox (NumA + NumB)