Sections 0101, 0102, 0103 and Honors, Fall 1995
Tuesday October 10, 1995
Assigned Reading: 5.4 - 5.5
Handouts (available on-line): none
Topics Covered:
- Traced through another example of function calls from a previous
midterm exam.
The question and
solution.
- Reviewed the importance of using function prototypes.
Here's a program that doesn't use
function prototypes. Strange output
is produced. The modified program
works correctly when the function prototype for add3 is put in
(sample run of the corrected program).
- When we use a function we should be careful to call the function
with parameters that are of the same type as declared in the
function's prototype. Here's a program
that violates this dictum; it lets the C compiler do the type
conversion. It works (see sample run),
but I consider this very poor programming style. If the C compiler
cannot convert the type of the actual parameter to the type
of the formal parameter, as in this program,
the compiler should give a compile time
error.
- A function that computes the value of a raised to power b.
Program and
sample run.
-
Using the function tested in the previous step, we write
a program
that rounds numbers within a given precision.
Sample run.
The meaning of functions like floor can be found
in the Unix manual pages for floor
by typing man floor.
-
If you typed man exp in UNIX, you would have noticed that there
is already a function (called pow) that computes a raised to the
power b. The manual entry for
"exp" begins with an explanation of "exp" for FORTRAN, ignore
this.
It is easy to modify our program which rounds real numbers so that
it uses the pow function from the math library. We simply
have our function call pow.
Program and
sample run.
- Returned Quiz 1.
Last Modified:
Thu Oct 12 08:48:53 EDT 1995
Richard Chang, chang@gl.umbc.edu