If we have an invocation in a return statement, things can get a little confusing, but only if the mechanism is not understood entirely. Here is an algorithm that tests your understanding of how “return” works.
This deceptively simple pseudocode generates the following trace. There is not much bulk to the trace, but certain transitions require a bit of explanation.
A | B | C | D |
E | F |
G | H | |
1 | comments | line# |
|
| ||||
2 | pre |
|
| |||||
3 | 9 | retinfo | n |
|
| |||
4 | 9: print (?) | 3 |
|
| ||||
5 | (n ≤ 1) is F | 3 |
|
| ||||
6 | 6 |
retinfo | n |
| ||||
7 |
6: return n*(?) | 2 |
| |||||
8 | (n ≤ 1) is F | 3 |
|
| ||||
9 | 6 |
|
retinfo | n | ||||
10 |
|
6: return n*(?) | 1
| |||||
11 | (n ≤ 1) is T | 3 |
|
| ||||
12 | 4 |
|
6: return n*(1) | |||||
When we execute line 4 of the pseudocode, the “retinfo” is copied, and the
return value (1) replaces the question mark. However, it is important not
to execute the solved statement in cell G12.
| ||||||||
13 | 8 |
|
retinfo | n | ||||
14 | 6 |
6: return n*(2) |
| |||||
We return to line 6 as per cell G12. Furthermore, line 6 looks like cell G12
at this point. Because columns G and H are deallocated already, “n” refers
to column F. As a result, the value to return is 2 × 1 = 2. Cell E7 is copied
and pasted, and the question mark in it is replaced by 2. Another way to
look at this is cell G12, the fully resolved line 6, is the “verb”, it tells us
what to do. Cell E7 is the “object”, the action specified by G12 is acting
on E7. Cell E14 is the “result” of the action.
| ||||||||
15 | 8 |
retinfo | n |
| ||||
16 | 6 | 9: print (6) |
|
| ||||
Again, we are on line 6 because the “retinfo” (cell E14) told us to continue
there. At this point columns E and F are deallocated. Line 6 is resolved as
stated in cell E14. As a result, “n” refers to column D. The pseudocode on
line 6 returns a value of 3 × 2 = 6, The “retinfo” cell (C4) is copied and
pasted. The question mark is replaced by 6. In other words, we have now
resolved the unknown of line 9 of the pseudocode. We do not execute line
9, however, at this point! Look at cell E14 as the “verb” that specifies what
to do. Cell C4 is the “object”, it is acted on. Cell C16 is the result of the
action.
| ||||||||
17 | 8 | retinfo | n |
|
| |||
18 | print 6 | 9 |
|
| ||||
We can now go back to line 9 as per cell C16. Furthermore, we also know
what to print now, as specified by cell C16.
| ||||||||
19 | post |
|
| |||||
It is particularly important to remember that the execution of a return statement only involves the following:
In other words, the execution of a return statement is actually very simple. The confusing part is what happens when we execute “end define sub”. At the end of a subroutine, we have to continue execution somewhere. In this case, the “retinfo” cell tells us where to continue execution. Furthermore, the “retinfo” cell will also have at least a part of it resolved. In other words, an invocation of a subroutine is replaced by a value.
The resolved “retinfo” cell becomes a verb, as it tells us “what to do”. If it is a “return” statement itself without any additional pending “invoke”, then it executes. As it executes, it is important to use the right context for any variable/parameter reference, as well as using the right “retinfo” cell to act on.