CSC 161 Grinnell College Spring, 2010
 
Imperative Problem Solving and Data Structures
 

Laboratory Exercises on Functions and Parameters

Goals

This laboratory exercise provides practice with procedures and parameters within C programs.

Steps for this Lab:

  1. Within a C program, define and use the following functions:

  2. Write a new version of your solution to Step 1, so that the program has just one procedure circleCompute that has three parameters, the radius of a circle, the circumference, and the area. circleCompute has a void return type, but takes the radius as input and returns the circumference and area as changed parameters. (You will need to pass in the addresses of the circumference and area variables from your main procedure.)

  3. 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, &y), and recompile and run. Again, explain why the resulting output occurs.
  4. 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.
  5. 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, ... .

Work to Turn In


This document is available on the World Wide Web as

     http://www.cs.grinnell.edu/~walker/courses/161.sp10/lab-c-functions.html

created 7 September 1998
last revised 22 February 2010
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.