Functions

Functions are broken into two parts: a function declaration (also called a prototype) and a function definition.

Function prototypes

Function prototypes are placed before the main program. Function prototypes have three parts

The argument list consists of a comma-separated list of types. The arguments are created when the function is called and destroyed when the function terminates.

Here are a few examples of protypes

double square_double(double);
float read_radius( );
void user_instruct( );
void display_results(double, double, double);

Function Definitions

Function definitions are placed after the main program. See the examples below for the definitions of the functions with the above prototypes.


There are two basic types of functions: those that return a value, and those that don't.

Functions that return a value

Functions that return a value are always part of another expression. They might be part of a formula

area = pi * pow(radius, 2);

or they might be the right hand side of an assignment statement

radius = read_radius(); //defined in the examples below

or they might be part of a cout statement

cout << sqrt(4.0);

or they can even be arguments to other function calls

discrim = sqrt( pow(b,2) - 4.0 * a * c );

The basic idea of a function that returns a value is that the actual function call (eg, sqrt(4.0)) gets replaced with the calculation that the function produces. So, to the program, the statement

cout << sqrt(4.0);

becomes

cout << 2.0;

In this way, the main program gets to use the value that the function calculates. So, functions that return a value affect the behavior of the main program.

Functions that don't return a value

Functions that don't return a value are used to print output statments

display_results(radius, area, circum); //defined in the examples below

or to print instructions to the user

instruct_user(); //defined in the examples below

The major point about these types of functions is that they don't affect the main program at all. After they are called, they are replaced with a void. So, to the program, the call

display_results (radius, area circum); //defined in the examples below

becomes

;

after the call. This seems a little strange, but that is what the void means. So, after the call is made and the function executes, the main program just continues as if nothing had happened. That is why void functions are used to print things out and to set things for the program environment.


Parameters - What are they and when are they needed?

Consider this function prototype

double square_double(double number);

It could be called in a couple of ways in the main program

double radius;
...
cout << square(radius);
double answer, radius;
...
answer = square(radius);
cout << square(4.0); double answer;
...
answer = square(4.0);

This function needs an argument because it requires a value from the main program in order to calulate its result. In each of these examples, a value is being sent to the function, either through a variable (number), or through a constant (4.0). It is not the name of the variable that is important, it is the value that is being sent that is important. So when you see a call to a function like cout << square_double(radius);, don't think of it as sending number to the function, but think of it as sending the value that is stored in number to the function.

Here is a common misconception about parameter passing. Students think that the name of the actual argument has to be the same as the name of the formal argument. This is not true. Perhaps an analogy will work. If you owe someone a dollar, you take the dollar out of your wallet and give the dollar to the other person. The other person then takes the dollar and puts it in his or her wallet. Each person has their own wallet. You do not give the other person your wallet. You exchange a dollar with the other person, not wallets. Furthermore, you don't have to have an identical wallet to the other person's wallet: any wallet will do. In this analogy, your wallet is the actual argument in the main program, and the other person's wallet is the formal argument in the function. The dollar in the analogy is the value that is being passed from the main program to the function. The value is taken out of the actual argument in the main program and given to the foramal argument  in the function. The names of the arguments do not have to be the same, they just have to have the same type.

Local variables

Local variables are declared in the function. Use them when the function needs some extra storage in order to do its work. The main point is that the extra storage doesn't have to be initialized with a value from the calling function.

Local Variables versus Formal Arguments

Local variables and formal arguments are very similar: they both exist only as long as the function is running. When the function is called, they are created. When the function ends, they are destroyed. The difference between them is in how they are initialized. Formal arguments are initialized with the value of the actual argument from the call to the function. Local variables are initialized inside the function.


These are the functions from Chapter 6 of the book, and copies that I have modified.

The circle program

The circle program with more of functions

The circle program with more of functions and two calculations. This program uses two separate functions to calculate the area and circumference of a circle.

The circle program with more of functions and two calculations, using a function with two reference parameters. If the area and circumference always need to be calculated together, then one function can be used to calculate two results. Such a function uses reference parameters and has a void return type.

Donut size program

Donut size program with more functions