Laboratory Exercises on Functions and Parameters
Goals
This laboratory exercise provides practice with
procedures and parameters within C programs.
Steps for this Lab:
-
Within a C program, define and use the following functions:
-
Function circum that takes a circle's radius as parameter and
returns the circumferences of the circle.
-
Function area that takes a radius as parameter and returns
the area of the circle with that radius.
-
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.)
-
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, &y), 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,
... .
Work to Turn In
-
Programs and sample runs for steps 1 and 2.
-
Observations of output and explanations for steps 3b-3c, 4b-4h, 5b-5e.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/161.sp09/lab-c-functions.html