UMBC CS 201,Spring 04
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-type is the datatype of the value returned by function.
- Name is the function's name
- pi's specify the functions parameters and give
the parameter's type and name
Return statement
- Form: return exp; or return;
- When evaluated, causes the function to stop and returns the
value of exp back to the calling function, or , in the second case, just
returns program contol back to the calling function.
CSEE
|
201
|
201 S'04
|
lectures
|
news
|
help
Tuesday, 20-Jan-2004 13:48:55 EST