1 5.3 sub procedures, part ii passing by value passing by reference sub procedures that return a...
TRANSCRIPT
![Page 1: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/1.jpg)
1
5.3 Sub Procedures, Part II
• Passing by Value
• Passing by Reference
• Sub Procedures that Return a Single Value
• Debugging
![Page 2: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/2.jpg)
2
ByVal and ByRef
• Parameters in Sub procedure headers are proceeded by ByVal or ByRef
• ByVal stands for By Value
• ByRef stands for By Reference
![Page 3: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/3.jpg)
3
Passing by Value
• When a variable argument is passed to a ByVal parameter, a copy of the value of the argument is passed.
• After the Sub procedure terminates, the original value of the actual parameter that was passed is unchanged, even if the procedure changes the formal parameter value.
![Page 4: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/4.jpg)
4
Passing by Reference• When a variable argument is passed to a
ByRef parameter, the actual parameter (argument) and the formal parameter are one and the same…they are the same location in memory.
• After the Sub procedure terminates, the original value of the actual parameter that was passed will be changed when the formal parameter is changed.
![Page 5: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/5.jpg)
5
Example 5.3.1
Sub procedure has a ByVal parameter.
![Page 6: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/6.jpg)
6
About to call.
Note the value in variable amt, the actual parameter (argument).
![Page 7: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/7.jpg)
7
In the called Sub procedure.
Formal parameter num has a copy of actual parameter amt.
![Page 8: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/8.jpg)
8
Formal parameter num’s value has changed.
![Page 9: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/9.jpg)
9
But the value in amt remains the same. This is because amt and val are two different memory locations.
![Page 10: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/10.jpg)
10
Therefore, any change to a ByVal formal parameter has no effect on its associated actual parameter.
![Page 11: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/11.jpg)
11
Example 5.3.1 (modified)
Let’s try the Sub procedure with a ByRef parameter.
![Page 12: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/12.jpg)
12
About to call.
Note the value in variable amt, the actual parameter (argument).
![Page 13: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/13.jpg)
13
In the called Sub procedure.
Formal parameter num is identical to actual parameter amt.
![Page 14: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/14.jpg)
14
Formal parameter num’s value has changed.
Formal parameter num’s value has changed.
![Page 15: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/15.jpg)
15
Since num is a ByRef parameter, the value in amt changes also. This is because amt and val
are the same memory location.
![Page 16: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/16.jpg)
16
Therefore, any change to a ByRef formal parameter remains in the
associated actual parameter after the called procedure terminates.
![Page 17: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/17.jpg)
17
Example 5.3.2
![Page 18: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/18.jpg)
18
Example 5.3.2
Sub (or Function) procedures with ByRef parameters can be used to “return” values. If you want a procedure to “return” more than one value,
you can use ByRef parameters for this.
![Page 19: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/19.jpg)
Example 5.3.3
19
![Page 20: 1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging](https://reader033.vdocuments.mx/reader033/viewer/2022052701/56649cb65503460f9497b76d/html5/thumbnails/20.jpg)
Example 5.3.3
20
If you want to swap the values in two variables, you need to have a temporary variable.