UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

Function Definition

The function definition header looks exactly like its function prototype, but without the semicolon. Following the function definition header is a block containing the function's statements, known as the function body.

Examples of function definitions

/*****************************************
** Function: Square()
** This function squares a number of type double and returns it.
**
** Inputs: a value to be squared
** Outputs: returns the square of the number provided
****************************************/

double Square (double n) 
{ 
    return (n * n);
}

/**************************************************
** Function: FindMinimum ()
**   This function finds the minimum of two integers
**   and returns the smaller of the two values.
**
** Inputs: integers n1 and n2 to be compared
** Outputs: returns the smaller of n1 and n2.
**          returns n2 if values are equal
**************************************************/
int FindMinimum (int n1, int n2) 
{
    int min;

    if (n1 < n2) 
    {
        min = n1;
    } 
    else 
    {
        min = n2;
    }

    return min;
}

General form

return-type Name (p1, p2, ... pn) 
{
    statements
}

Return statement


CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 22-Aug-2006 07:14:16 EDT