Laboratory Exercises For Computer Science 213

Laboratory Exercises on Procedures and Parameters

Goals: This laboratory exercise provides additional practice with C programs procedures and parameters.

Steps for this Lab:

  1. Review sample programs 1-23 in An Introduction to C Through Annotated Examples, as discussed in class.

  2. Consider the program /home/walker/c/examples/lab2-1.c.
    1. Copy this program to your account.
    2. Compile and run this program. Explain each value printed.
    3. Edit out the address operation & in the call pr (x, &), and recompile and run. Again, explain why the resulting output occurs.

  3. Consider the program /home/walker/c/examples/lab2-2.c.
    1. Copy this program to your account.
    2. Compile and run this program. Explain each value printed.
    3. 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?
    4. 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?
    5. 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.
    6. Replace the line *a = *b; by the statement a = b;. Again, recompile and rerun, and explain the resulting output.
    7. 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.
    8. Change *a = b; back to *a = *b;, and change the subsequent assignment *b = 6; to *a = 6;. Again, predict, recompile, rerun, and explain.

  4. Consider the program /home/walker/c/examples/lab2-3.c.
    1. Copy this program to your account.
    2. Compile and run this program. Explain each value printed.
    3. Within the printf statements, change each *s to s and each *b to b. Recompile, rerun, and explain, as before.
    4. Within prA, change each *s to s. Recompile, rerun, and explain.
    5. 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

created September 7, 1998
last revised Septemmber 17, 2001
Valid HTML 3.2!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.