You can compile all the example programs with simple commands like:
g++ -o p p.cppand run like this
./pwhere p.cpp is the name of a program.
A program with functions called inside other functions
Down load
[ fun1.cpp ]
and compile and run it.
One function is "instrumented" to show you what is happening. It outputs something when it starts and something else when it exits/returns.
Do something similar to all the other functions so that we can get a complete listing of every function. Treat Main as another function as well.
Compile test and run and learn how control is passed down into a
function and then comes out again...
Functions with parameters and return values
Download
[ fun3.cpp ]
and look at it. Work out on a sheet
of paper what it will do if you compile and run it.
Then compile and run it. Were you right or has something unexpected happened? What can you learn from this?
Functions with string parameters...
The following example shows (1) strings are OK as parameters, (2) how to
make the instrumented outputs show the nesting of the
function calls:
[ fun2.cpp ]
Can you explain why the strings move to the right with each
level of calling?
Functions with reference parameters
Here we use pass-by-reference to change a parameter:
[ ref.cpp ]
Can you predict what will happen?
Try out the experiments suggested in the comments in the code.
Debug a bad function
Here
[ bad.cpp ]
is a deliberately broken function embedded in a test program.
Don't change the main program, but fix the errors in the
code.
Credit
Show the lab teacher the changes you have made and talk about
what you have learned.