The program ``rods.c'' is divided into six functions, named square,
distance, cos_A_from_ABC_triangle,
x_coordinate_of_rod, y_coordinate_of_rod, and main.
You are already familiar with the role of the main function. When the
computer executes the program, it starts carrying out the statements in the
main function.
The rest of the program defines new functions that are useful in implementing
main. Every C function starts with a line that describes the type, name,
and arguments to the function. For example, the function square starts
with
float square(float x)
That line tells the compiler that we're defining a new function. The function
is named square, takes one float (floating-point) argument named
x, and returns a float result value.
When the square function is invoked, the argument to
the function is assigned to the variable x. For example, when the
square function is invoked by the statement:
result = square(5.0);
the value of x inside square is 5.0.
Now let's talk about the body of the square function. The body starts
with an opening curly brace `{' and ends with a closing curly brace
`}'. Within the body there is only one statement:
return (x * x);
This return statement tells the computer to stop executing statements
inside the square function, go back to the place at which the
square function was invoked, and resume executing statements there. The value
listed in the return statement--(x * x)--is the value that
is returned by the square function.
In C, the return statement does two things. It specifies the value that
will be returned from the function, and it causes the computer to go back to
the point in the program at which the function was invoked. In other words,
the return statement returns both a value and ``program control'' to the
statement that invoked the function.
Eric N. Eide
Hamlet Project
Department of Computer Science
University of Utah