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
printEquationhas avoidreturn type. Parameters are passed intoprintEquation, but nothing is passed back. What will happen if the statement
is inserted at the end of this procedure?return coeff2+coeff1+coeff0; - Verify your prediction by making the modification and compiling the code. Explain the result.
- Restore the program to its original state.
-
Procedure
printEquationis called in both proceduresdisplayExample1anddisplayExample2with just a simple statement, such as:
Predict what will happen if you try to assign the result ofprintEquation (1.0, -3.0, 2.0);printEquationto a variable, such as
After making your prediction, verify whether you were correct by making the change and compiling the code. Explain the results.double value = printEquation(1.0, -3.0, 2.0); - Restore the program again to its original state.
-
Procedure
computeDiscriminantreturns adouble. Sometimes beginning programmers separate the call to a function from the assignment of the return value. Here are two examples:Attempt 1:
double discriminant; computeDiscriminant (a, b, c);Attempt 2:
computeDiscriminant (a, b, c); double discriminant;Try each of these attempts within procedure
printRoots.- Does the code compile?
- First, use "
make" to compile. -
Next, try using the following command to manually
compile your modified program:
clang -lm -o quadratic quadratic.c
- First, use "
- Does the compiled program give the desired result?
- Does the code compile?
-
Change the body of
computeDiscriminantto the following
Compile and run the program.printf ("r = %lf, s = %lf, t = %lf\n", r, s, t); return sqrt (s*s - 4*r*t);- 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.,
computeDiscriminant)?
-
Procedure
Writing Numeric Functions
-
Within a new C program, define and use the following functions:
-
Function
computeCircumferencethat takes a circle's radius as parameter and returns the circumference of the circle. -
Function
computeAreathat takes a radius as parameter and returns the area of the circle with that radius.
M_PIby including the library headermath.h. Alternately, one can define their own constant or implicitly calculate the value with an inverse trigonometric function. -
Function
The Perils of Global Variables
Global variables are variables declared outside any
particular function. In terms of our stack model of computation,
they exist in a separate part of memory called the static
region, which you can draw in your diagrams as a collection of
variable-value pairs separate from the stack. They tend to be
problematic and are generally to be avoided (unless they are declared
constants with the
keyword const. Instead, you should
favor explicitly passing data between function calls using parameters
and return values.
-
To better understand the confusions global variables can cause, consider the
following program.
#include int glob = 10; void g (void) { // Point (B)---first call to g() for (int i = 0; i < glob; i++) { printf("%d ", i); } printf("\n"); glob += 1; // Point (C)---final call to g() } // g int f (void) { for (int i = 0; i < glob; i += 2) { // Point (A)---first iteration of the for-loop g(); printf("===\n"); } return glob; } // f int main (void) { int ret = f(); printf("%d\n",ret); // Point (D) } // main - Predict what the program will print.
- Verify your prediction by compiling and running the code. If your prediction was incorrect, trace the execution of the program by recording and updating the stack diagram (including the static region) as you trace the code's execution.
Functions, Values as Parameters, and Robot Motion
Program yoyo.c uses the
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
byresult = yoyo (repetitions);
Does making this call twice change how many times the robot yoyos in each call? Why or why not?repetitions = yoyo (repetitions); repetitions = yoyo (repetitions); -
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.
-
Explain what the program does.
-
Recall the
reading's discussion of
memory diagrams. Draw a sequence of stack diagrams for
program
quadratic.cfrom Step 1.Important: To benefit from this exercise, you must actually draw these diagrams. It is not a mental exercise.
-
Consider the program
value-params.c.- Copy the program to your account, compile, and run it.
-
Draw stack diagrams for the program state when
each
printstatement is executed. Be sure you can explain the running of the program at each step, based upon this series of diagrams.
