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.) - Restore the program to its original state.
-
Procedure
printEqnis called in both procedureseqn1andeqn2with just a simple statement, such as:
What happens if you try to assign the result ofprintEqn(1.0, -3.0, 2.0);printEqnto a variable, such as
(Try this and explain what happens.)double value = printEqn(1.0, -3.0, 2.0); - Restore the program again to its original state.
-
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 discriminant; disc(a, b, c);Attempt 2:
disc(a, b, c); double discriminant;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 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.,
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?
-
Procedure
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.
-
Function
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 schematic memory 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 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.
