Functions
Goals
This laboratory exercise provides initial practice with functions and passing values as parameters within C programs.
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
-
Copy
quadratic.cto your account, compile and run it, and review the code to determine how it works.-
Procedure
printEqnhas avoidreturn type. Parameters are passed intoprintEqn, but nothing is passed back. What happens if the statementreturn a+b+c;is inserted at the end of this procedure? (Try compiling the code and explain what happens.) -
Procedure
printEqnis called in both procedureseqn1andeqn2with just a simple statement, such as:printEqn(1.0, -3.0, 2.0);What happens if you try to assign the result of
printEqnto a variable, such asdouble value = printEqn(1.0, -3.0, 2.0); -
Procedure
discreturns adouble. 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?
-
Change the body of
discto the followingprintf("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
printfstatement in functiondisc, insert the code from Step 1c (intoprintRoots). What happens when procedure with a return value (e.g.,disc) is called on a line by itself?
-
Writing Numeric Functions
-
Within a C program, define and use the following functions:
-
Function
circumthat takes a circle's radius as parameter and returns the circumferences of the circle. -
Function
areathat takes a radius as parameter and returns the area of the circle with that radius.
-
Functions, Values as Parameters, and Robot Motion
Program yoyo.c uses a
function yoyo to control a Scribbler 2 robot.
-
Copy
yoyo.cto your account, compile and run it, and review to code to determine how it works.-
Explain what the program does.
- What movements does the robot make? Why?
- What output is printed? Why?
-
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?
-
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?
-
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 therepetitionsvariable torepsthroughout themain procedure. Compile and run the program.Does changing
yoyo's variablerepshave any impact on the variablerepsin themainprocedure? -
Again, return to the original program. This time nest the call to
yoyoin themainprocedure. That is, the call toyoyowill 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.
To clarify, consider the
original yoyo.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.
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).
When yoyo is finished, its space is deallocated, and the returned
value is stored in variable result.
-
Draw a sequence of schematic memory diagrams for program
quadratic.cfrom Step 1. -
Consider the program
value-params.c.-
Copy the program to your account, compile, and run it.
-
Draw schematic memory diagrams for the state of memory when each
printstatement is executed. Be sure you can explain the running of the program at each step, based upon this series of schematic memory diagrams.
-
