## Lecture 8: Preview of Functions

**Tuesday, September 27, 2011**

*Status: preliminary*

[Up] [Previous Lecture] [Next Lecture]

**Powerpoint Slides:** none

**Reading Assigned:** 3.1-3.2

**Homework Due:** Homework 3

**Homework Assigned:** Homework 4

**Topics Covered:**

- Math library functions (examples)
- Overview of functions calls
- Arguments and return values
- Function prototypes and header files (basic)

**Programs Shown:**

- This program uses some functions from the math library:
math1.c
To compile this program, you need to use the
`-lm`flag:gcc -Wall math1.c -lm

- This program calculates the area of a washer (the kind
that you use with nuts and bolts, not the kind that cleans
your clothes): washer1.c.
To do so, it has to compute the area of a circle twice. We can
put that computation in a separate function named
`circleArea()`. -
If we put the
`circleArea()`function at the end of the file instead of the beginning, then we have to add a function prototype at the top: washer2.c. -
Finally, the main program itself is actually a function. Strictly
speaking,
`main()`returns an`int`value. This tells the operating system if there was an error. In the third version of the program, we fix up`main()`: washer3.c. Now we can compile with the`-Wall`option and not encounter any warnings or errors. - If we used
`gcc -Wall`on the buggy scanf3a.c program, we would have know about the mistake of using`%f`instead of`%lf`.

DO NOT COPY THIS PROGRAM. IT IS DELIBERATELY BUGGY! - Another example, this program has a function that computes the
average of two values: avg1.c.
Yes, functions can have many input values (a.k.a. formal parameters).
- Here we mix things up and combine the
`circleArea`function from above with the`avg()`function: avg2.c.