Function Definition

  1. A function definition describes in detail what the function does, how it uses its parameters to compute the value that it returns to the calling function.

  2. A function definition begins with a function header followed by a function body.

  3. The syntax for the function definition is:

    Type_Returned Function_Name (Type1 Parameter_name1, Type2 Parameter_Name2, ...)
    {
    Local Variable Declarations
    Function Statements
    ...
    return Function_Value;
    }

  4. The header of the function definition has the same form as the function prototype, except that the parameter names must be included, and the header has no semicolon at the end.

  5. The body of the function definition is enclosed by a pair of braces and contains local variables declarations and executable statements.

  6. The function parameters are referred to as formal parameters because they are used as blank placeholders to stand for the actual arguments which are passed into the function when the function is called.

  7. The value returned by a function must be specified using a return statement of the form:

    return Expression;

  8. When a return statement is encountered, the Expression is evaluated, execution of the function is stopped, and the value of the Expression is returned to the function that called the present function.

  9. The value returned must agree with the type declared and the type specified in the header of the function definition.

  10. Program control is also passed back to the calling function where the function call took place.

  11. Example of a function definition for the function that computes the area of a circle.

    double CircleArea (double Radius)
    {
         double PI = 3.14159265358979;
         double area;
         area = PI * Radius * Radius;
         return area;
    }