Illustrating transfer of control:
Examples: transferControl.c,
bottles.c, morse.c
(morse.c one passes values to functions, which we don't know about yet...)
Process: Draw enclosing boxes around each function, including main() and
the entire program. When looking for a variable, look in the current box or
outside the current box. You cannot look into another box.
Parameters
Passing in values using parameters
E.g. Pass two numbers to a function to be added: parameters1.c
Call by value vs. call by reference
Program to swap 2 numbers
swap1.c:
version in C++
In C++ to have a parameter's changed value returned to the caller
(i.e. it is a reference parameter), all that is needed is to put an
ampersand '&' between the parameter type and the parameter name.
swap2.c:
version in C
In C if you want a parameter's changed value to be reflected back
to the calling program you must do the following three things:
Again consider the program parameters1.c
that we looked at above. How would you change this to return the answer as
a parameter rather than using the function's return value? Answer: parameters2