7.4.1 How it works
A call writes the name and, inside required parentheses, the arguments. The arguments are computed from left to right and only once, and must match the parameters in number and type.
Every call creates a frame: a space of its own for its parameters and local variables. When it ends, the frame disappears and control goes back to the caller.
1
Principalis running with its frame.2
Doble(21)pushes a new frame withn= 21.Principalwaits.3
Doblereturns 42: its frame disappears andPrincipalcarries on with the value.
A stack with the Principal frame at the bottom. Calling Doble pushes a frame with n = 21; on return it disappears and the value 42 goes down to Principal.
A subprogram that returns a value is used as an expression, where a value is needed. One that returns nothing is used as a statement. Mixing them up has different consequences:
| Situation | Result |
|---|---|
| using as a value something that returns nothing | error |
| calling as a statement something that returns a value | warning: the value is lost |
ProgramA discarded value and one argument too many
Subroutine Doble(n As Integer) As Integer
Return n * 2;
EndSubroutine
Algorithm Principal
Doble(3);
Write Doble(2, 3);
EndAlgorithmDiagnostics
discardedReturnValue· warning · 6:3 · Return value from subroutine Doble is discardedargumentCountMismatch· error · 7:9 · Incorrect number of arguments: expected 1 but found 2
The program finishes with an error.
In short
- Parentheses are required; arguments are computed from left to right.
- Number and types must match the signature.
- Every call has its own frame, which disappears on return.
- Discarding a returned value is a warning.