Charles E. Oyibo
In C++, the concept of a function (otherwise called a module), either predefined or user-defined, is similar to that of a function in algebra. Every function has a name and, depending on the values specified by the use, it does some computation.
Let us consider the predefined mathematical function pow(x, y) which calculates xy:
We explore some common predefined functions below. An expansive list of predifined functions is contained in Appendix F of Malik.
| Function | Standard Header File | Purpose | Parameter Type(s) | Result |
|---|---|---|---|---|
| abs(x) | <cstdlib> | Returns the absolute value of its argument | int | int |
| ceil(x) | <cmath> | Returns the smallest whole number that is greater than or equal to x | double | double |
| cos(x) | <cmath> | Returns the cosine of angle x | double (radians) | double |
| exp(x) | <cmath> | Returns ex, where e = 2.718: | double | double |
| fabs(x) | <cmath> | Returns the absolute value of its argument | double | double |
| floor(x) | <cmath> | Returns the largest whole number that is less than or equal to x | double | double |
| pow(x, y) | <cmath> | Returns xy; if x is negative, y must be a whole number | double | double |
| tolower(x) | <cctype> | Returns the lowercase value of x if x is uppercase; otherwise returns x | int | int |
| toupper(x) | <cctype> | Returns the uppercase value of x if x is lowercase; otherwise returns x | int | int |
To use predefined functions in a program, we must include the header file that contains the functions specifications via an include statement. To use the function pow, for example, the program must include:
#include <cmath>
User-defined functions in C++ are classified into two categories:
To use value-returning functions in our program, we must know the name of the header file that contains the function's specification. We need to include this header file in our program using the include statement and know the following items:
Because the value returned by a value-returning function is unique, we use the value in one of three ways:
Hence, we see that a value-returning function is used:
That is, value-returning functions are used (called) in expressions.
The four properties of a user-defined, value-returning function given above form the heading of the function (also called the function header); there is however one more thing associated with functions (both value-returning and void):
which forms the body of the function. Together these five properties form the definition of the function. Also, the variable(s) declared in the heading of a function are called the formal parameter(s) of the function, in constrast to actual parameter(s)--variable(s) that appear in a call to the function.
Suppose that the heading of the function pow is:
double pow(double base, double exponent)
and that a function that calls function pow takes the form:
double u = 2.5
double v = 3.0;
x = pow(u, v);
The function pow is said to be called by u and v (the actual parameters), and the values of u (2.5) and v (3.0) are copied to base and exponent (the formal parameters) respectively.
So, the formal definitions:
Formal Parameter: A variable declared in the function heading.
Actual Parameter: A variable or expression listed in a call to a function
functionType functionName(formal parameter list)
{
statements
}
where statements are usually declaration statements and/or executables. Here, functionType is the type of the value that the function returns. The functionType is also called the data type or the return type of the value-returning function.
The syntax of the formal parameter list (enclosed in parenthesis in the value-returning function syntax) is:
dataType identifier, dataType identifier, ...
The syntax to call a value-returning function is:
functionName(actual parameter list)
The syntax of the actual parameter list (enclosed in parenthesis in the function call syntax) is:
expression or variable, expression or variable, ...
A functions formal parameter list can be empty. If the formal parameter list is empty, the parenthesis are still needed:
functionType functionName()
If the formal parameter list of a value-returning function is empty, the actual parameters in a function call must also be empty. In this case, the (empty) parenthesis are still needed.
functionName()
Furthermore, the number of actual parameters, as well as their data types must match with formal parameters in the given order. That is, actual and formal parameters must have a one-to-one correspondence.
Once a value-returning function computes the value, the function returns this value via the return statement. That is, it passes this value outside the function via the return statement:
return expr;
where expr is a variable, constant value, or expression. The expr is evaluated and its value is returned. The data type of the value that expr computes must match the function type.
When a return statement is executed in a function, the function immediately terminates and the control goes back to the caller (or calling function). Moreover, the function call statement is replaced by the value returned by the return statement. When a return statement executes in the function main, the program terminates.
Consider the following function that calculates the larger of two numbers. (Note that x and y are formal parameters):
double larger (double
x, double y)
{
double max
if (x>=y)
max = x;
else
max = y;
return max;
}
which is exactly equivalent to
double larger (double
x, double y)
{
double max
if (x>=y)
return x;
else
return y;
}
Now, consider the following lines of code in function main that illustrate how to call function larger (Note that the the constants 5 and 6, and the variables one and two are actual parameters):
int main()
{
double one, two, maxNum;
cout << "The larger of 5 and 6 is" << larger (5, 6) <<
endl;
cout << "Enter two numbers: "
cin >> one >> two;
cout << endl;
cout << "The larger of " << one << " and "
<< two
<< " is " << larger(one, two) << endl;
return 0;
}
Following the rules that we must declare an identifier before we can use it, and knowing that the function main in the example above uses the function larger, it would make sense to place larger before main.
In reality, C++ programmers customarily place the function main before all other user-defined functions. To work around the problem of undeclared identifiers (since the function that declares the identifier is placed after main), we place function protypes, the function heading without the body of the function, before any function definition (including the definition of main). The syntax of a function prototype is:
functionType functionName(parameter list);
So that the protoype for the function larger is:
double larger (double x, double y);
Note that the function prototype ends with a semicolon.
A value-returning function must return a value for the function to behave properly. Furthermore, a return statement returns only one value, even if the return statement contains more than one expression (as in return x, y;); only the value of the last expression, y, is returned.
See Example 6-3: Palindrome Number; p. 284-6, Malik
Programming Example: Cable Company p. 289-94, Malik.
Page Last Updated: Saturday February 12, 2005 10:21 AM