Laboratory Exercises on Procedures and Parameters
Goals: This laboratory exercise provides additional practice with C
programs procedures and parameters.
Steps for this Lab:
-
Review sample programs 1-23 in An Introduction to C
Through Annotated Examples, as discussed in class.
-
Consider the program /home/walker/c/examples/lab2-1.c.
-
Copy this program to your account.
-
Compile and run this program. Explain each value printed.
-
Edit out the address operation & in the call pr (x, &), and
recompile and run. Again, explain why the resulting output occurs.
-
Consider the program /home/walker/c/examples/lab2-2.c.
-
Copy this program to your account.
-
Compile and run this program. Explain each value printed.
-
Add the declaration int w = 100; as the first statement in
the main procedure (before the declaration int x = 3;).
Recompile and rerun your program. Does the output change? Explain. Does
the result depend upon the value assigned to w? Why or why not?
-
Add the declaration int z = 25; immediately after the declaration
of y in main. Recompile and rerun. Again, does the
output change? Why or why not?
-
Within the printf statements for pr, change each
a to *a and each b to *b. Recompile,
rerun your program, and explain the resulting output.
-
Replace the line *a = *b; by the statement a = b;.
Again, recompile and rerun, and explain the resulting output.
-
Replace the same line (now a = b;) by the statement *a =
b;. Try to predict what will be printed. Then recompile, rerun
the program, and explain what happens.
-
Change *a = b; back to *a = *b;, and change the subsequent
assignment *b = 6; to *a = 6;. Again, predict,
recompile, rerun, and explain.
-
Consider the program /home/walker/c/examples/lab2-3.c.
-
Copy this program to your account.
-
Compile and run this program. Explain each value printed.
-
Within the printf statements, change each *s to
s and each *b to b. Recompile, rerun, and
explain, as before.
-
Within prA, change each *s to s. Recompile,
rerun, and explain.
-
Within prA, change each r to *r. Recompile,
... .
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/195.fa01/lab.C-functions.html