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

Laboratory Exercises on Functions and Parameters, Part 1

Goals

This laboratory exercise provides initial practice with procedures and passing values as parameters within C programs.

Preparation before Class

Introduction

As with Scheme, a function or procedure in C is designed to perform a task. Several variations are possible, such as the following:

Altogether, a function or procedure performs some work. Variations arise in whether or not data are needed to start the work and whether or not computed results are returned to the main program.

Work Started in Class

Consider the program quadratic.c that solves the quadratic formula ax2 + bx + c = 0, while illustrating several common variations for simple functions.

void and non-void Functions

  1. Copy quadratic.c to your account, compile and run it, and review the code to determine how it works.

    1. Procedure printEqn has a void return type. Parameters are passed into printEqn, but nothing is passed back. What happens if the statement return a+b+c is inserted at the end of this procedure? (Try compiling the code and explain what happens.)

    2. Procedure printEqn is called in both procedures eqn1 and eqn2 with just a simple statement, such as:

        printEqn   (1.0, -3.0, 2.0);
      

      What happens if you try to assign the result of printEqn to a variable, such as

        double value = printEqn   (1.0, -3.0, 2.0);
      
    3. Procedure disc returns a double. Sometimes beginning coders separate the call to a function with the assignment of the return value. Here are three examples:

      Attempt 1:

        double discriminate;
        disc (a, b, c);
      

      Attempt 2:

        disc (a, b, c);
        double discriminate;
      

      Try each of these attempts within procedure printRoots.

      • Does the code compile?
      • Does the code give the desired result?
    4. Change the body of disc to the following

        printf ("r = %lf, s = %lf, t = %lf\n", r, s, t);
        return sqrt (s*s - 4*r*t);
      

      Compile and run the program

      • Describe what is printed by the overall program. What might you conclude about what happens when printing is done in the middle of a computation? For example, how does this impact the overall program's output?
      • Can you identify any guidelines regarding the advisability of including print statements in functions designed for computation (e.g., disc)?
      • In addition to the printf statement in function disc, insert the code from Step 1c (into printRoots). What happens when procedure with a return value (e.g., disc) is called on a line by itself?

Writing Numeric Functions

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

Functions, Values as Parameters, and Robot Motion

Program yoyo-program.c uses a function yoyo to control a Scribbler 2 robot.

  1. Copy yoyo-program.c to your account, compile and run it, and review to code to determine how it works.

    1. Explain what the program does.

      • What movements does the robot make? Why?
      • What output is printed? Why?
    2. In the main program, duplicate the line

        result = yoyo (repetitions);
      

      (I.e., this line should appear twice in succession.)

      Does making this call twice change how many times the robot yoyos in each call? Why or why not?

    3. In the main program, replace the line

        result = yoyo (repetitions);
      

      by

        repetitions = yoyo (repetitions);
        repetitions = yoyo (repetitions);
      

      Does making this call twice change how many times the robot yoyos in each call? Why or why not?

    4. In C (as in Scheme), variables declared within a function are separate from variables declared elsewhere (e.g., in main). In the original program, change the name of the repetitions variable to reps throughout the main procedure. Compile and run the program.

      Does changing yoyo's variable reps have any impact on the variable reps in the main procedure?

    5. Again, return to the original program. This time nest the call to yoyo in the main procedure. That is, the call to yoyo will become:

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

      • Test out your nested call and explain what happened.

Schematic of Memory

When analyzing a C program, it often is helpful to create a schematic of a computer's memory. Overall, each procedure requires memory for its parameters and any local variables. This amount of space is allocated whenever the procedure is called, and this space is deallocated when the procedure finishes.


main

To clarify, consider the original yoyo-program.c program. When the program begins, space is allocated for the variables, repetitions and result in the main procedure. The assignment repetitions = 2; stores the number 2 in the location for the repetitions variable.



yoyo

When the yoyo procedure is called, new space is allocated for the parameter count and for local variables i and reps. During the call to yoyo, the value of the variable repetitions is copied into the space for the parameter count. That is, the value 2 is now stored in two places (e.g., repetitions and count).

When yoyo executes, reps is given the value 6. The variable i takes on successive values 0, 1, 2, 3, 4, 5, and 6.

As yoyo finishes, it returns the value of reps (6).


main

When yoyo is finished, its space is deallocated, and the returned value is stored in variable result.



  1. Draw a sequence of schematic memory diagrams for program quadratic.c from Step 1.

Homework

  1. Consider the program lab-value-parm.c.

    1. Copy the program to your account, compile, and run it.

    2. Draw schematic memory diagrams for the state of memory when each print statement is executed. Be sure you can explain the running of the program at each step, based upon this series of schematic memory diagrams.

Feedback Welcome

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