CSC 161 Grinnell College Spring, 2013 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:

This lab has two parts:

• Practice with functions, value parameters, and robot motion.
• Exploration of value and reference parameters

## Functions, Value Parameters, and Robot Motion

1. Make a file named hoedown.c and set it up in the standard way.

2. Copy the following function into your program and describe what it does:

```int
yoyo (int count)
{
int i;
int reps = 3*count;

for (i = 0; i < reps; i++)
{
rForward (1, .5);
rBackward (1, .5);
}

sleep (3);

return reps;
} // yoyo```
3. Explore using the yoyo procedure as follows:

• Make an int variable, repetitions, set it to 2, and use it to call yoyo. What does calling the following call do?

```int repetitions = 2;
yoyo (repetitions);
printf ("repetitions = %d\n", repetitions);```
• Does making this call twice change how many times the robot yoyos in each call?

• Now replace the above code with the following variation:

```int repetitions = 2;
repetitions = yoyo (repetitions);
printf ("repetitions = %d\n", repetitions);```
• Explain what happens.

• Does making this call twice change how many times the robot yoyos in each call?

• Explain the difference between the two calls?

4. Consider the nesting of calls yoyo in the following manner:

```
yoyo (yoyo (repetitions));```
• Make a prediction of what will happen with the above nested call.

• Test out your nested call and explain what happened.

## Value and Reference Parameters

1. Look over amp-examp.c and write a few sentences explaining what it does.

2. 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.

3. Write a new version of your solution to Step 6, 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.)

4. 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.
5. 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.

## Feedback Welcome

Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.