A natural question regarding passing by reference is: what happens when a parameter that is passed by reference is passed again by reference to another subroutine? The following pseudocode illustrates this.
This code yields the following trace.
A | B | C | D | E | F | G | explanation |
|
1 | line# |
|
||||||
2 | pre |
|
||||||
3 | 13 | retline# | m | “m” is a local variable. |
||||
4 | post | ? |
|
|||||
5 | 11 | retline# | i | “m” is passed by reference to parameter “i”. |
||||
6 | 12 | aka col C | As a result, “i” is an alias to the column that corresponds to “m”. |
|||||
7 | 7 | retline# | x | “i” is passed by reference again to parameter “x”. |
||||
8 | 8 | aka col C | “x” is not an alias of column E, but rather an alias of column C! This means that when a byref parameter is passed by reference, the reference is passed along. “x” is not an alias of an alias (or a reference to a reference). One can also say that both column E (“i” of “relay”) and column G (“x” of “reset”) are aliases of column C at the same time. |
|||||
9 | 3 | 0 |
| |||||
10 | 4 | retline# | x | |||||
11 | 8 | retline# | i |
|
||||
12 | 12 | retline# | m |
|
||||
13 | post |
|
||||||