next up previous
Next: About this document ... Up: Simple Functions: Previous: Simple Functions:

What is New Here?

Here we explain the new features in the code

The subprogram for evaluating the function takes only three lines. Here is its definition:

double f(double x){
   return 4*x - cos(x);

The subprogram for the derivative is similarly short. We put their definitions at the top of the code as a matter of convenience. They are not executed until they are needed. As always, execution begins in the main program.

The general pattern for the definition is

type function-name(function-argument-list){
The definition of a function has the block of function statements. The first line also serves to declare the function type. In the above example the functions (identifiers) f and dfdx are given type double, so it makes sense later on to include them in an arithmetic expression in the natural way:

    pnew = p - f(p)/dfdx(p);

Logically, in order to assign a new value to pnew the compiler realizes that to evaluate the expression f(p)/dfdx(p) in the main program, it has to hand over the current value of p to the subprogram f, which determines the value of the function for that value of x and hands the result back to the main program. The compiler does the same for the derivative. Finally, it divides and subtracts the result from p. We say that the main program ``calls'' the function subprogram. The subprogram does its work and and then returns control to the calling program.

The result of f(p) is called its ``return value''. The return value is of type double as specified by double f. The same is true of dfdx(p). The return statement in a function definition has the general form

  return expression;
It causes the expression to be evaluated and the result handed back as the return value of the function.

The expression can be a single variable. We could have written the function this way:

double f(double x){
   double y;

   y = 4*x - cos(x);
   return y;

The function argument list (sometimes called the ``parameter list'') lists input and/or additional output values for the function. It declares their types as well. In this case we have only one input value of type double.

The function argument list is actually a series of declarations. When there are more than one, the list is separated by commas. Here is what it looks like with two parameters.

double f(double x, int c){
   return c*x - cos(x);
Of course, in that case we would call the function with
to get the same result as before.

We say that when the function is called, the input parameters are ``passed'' to the function. For the input parameters we can think of this operation as an implied assignment. In this example we have, in effect,

   x = p;
   c = 4;
In the next lesson we make more precise sense of such implied assignments when we discuss the concept of the ``scope'' of a variable's definition. Suffice it to say, here, that the names on the lhs belong to the subprogram and the names on the rhs belong to the calling program. Here there is no confusion because the names are different. But if the names were the same, this qualification would be needed to make sense of the assignment.

The names of the subprogram arguments may differ from the names in the calling program, but the purpose and data type must match in the order given. Of course the compiler can handle assignments that convert among the base numeric types float, double and int. For your protection it may complain or warn you about converting floating point to integer, however, which might cause a loss of significance or a truncation of leading digits.

The implied assignment can involve expressions. In the example we could have written sqrt(b*b - 4*a*c). The compiler evaluates the expression and passes the result to sqrt.

We will discuss function subprograms in greater depth in the next lesson.

next up previous
Next: About this document ... Up: Simple Functions: Previous: Simple Functions:
Carleton DeTar 2007-08-17