Charles E. Oyibo
In Ch 6, we discussed user-defined value-returning functions; we will now cover user-defined void functions.
A void function does not have a data type; therefore we do not include functionType (return type) in the heading of a void function. However, in a void function, we can use the return statement without any value, typically to exit the function early. Like value-returning functions, void functions may or may not have formal parameters.
Because void functions do not have a data type, they are not used (called) in an expression. A call to a void function should be a stand-alone statement.
void functionName()
{
statements
}
where statements are usually declaration and/or executable statements.
functionName();
Because these void functions do not have parameters, no values can be passed to them (unless we use global variable, defined later in this chapter). Such function are thus usually good only for displaying information about the program or printing statements.
One of the limitation to void functions without parameters is that no information can be passed in and out of such functions. The communication link between the calling function and the called function is established by using parameters.
void functionName (formal parameter list)
{
statements
}
where statements are usually declaration and/or executable statements.
dataType& variable, dataType& variable, ...
For a void function with parameters, we must specify both the data type and the variable name in the formal parameter list. The symbol & after the dataType is optional, has a special meaning, and will be discussed shortly.
functionName (actual parameter list);
expression or variable, expression or variable, ...
where expression can consist of a single constant value. As with a value-returning function, in a function call, the number of actual parameters together with their data types must match the formal parameters in the order given (that is, with a one-to-one correspondence).
Parameters provide a communication link between the calling function (such as main) and the called function. They enable functions to manipulate different data each time they are called. In general, there are two types of formal parameters: value parameters and reference parameters.
A value parameter is a formal parameter that receives a copy of the content of the corresponding actual parameter.
A reference parameter is a formal parameter that receives the location (memory address) of the corresponding actual parameter.
When we attach a & after the dataType in the formal parameter list of a function, the variable following that dataType becomes a reference parameter.
Consider:
void expFun (int
one, int& two, char
three, double& four)
{
...
}
The function expFun has four parameters:
See Sample program on p. 311 of Malik. Do walk-through.
We've discussed value parameters and reference parameters in the preceding section. When a function is called, the value of the actual parameter is copied into the corresponding formal parameter, so that during program execution, the formal parameter manipulates the data stored in its own memory space.
Memory is allocated to the value parameters when values are passed from the actual parameters (in the calling function), and deallocated at the end of the function.
Because a reference parameter receives the address (memory location) of actual parameter, reference parameters can pass one or more values from a function and can change the value of the actual parameter. Reference parameters are useful in three situations:
Recall: when we attach & after the dataType in the formal parameter list of a function, the variable following the dataType becomes a reference parameter.
See Examples on pages 302 to 333 in Malik.
While we can use reference parameter in value-returning functions, it this approach is not recommended.By definition, a value-returning function returns a single value via the return statement. If the function needs to return more than one value, we should change it to a void (i.e. non-value-returning) function and use the appropriate reference parameters to return the values.
The scope of an identifier refers to where in the program an identifier is accessible (visible).
Recall that an identifier is the name of something in C++ such as a variable or function name.
Local identifiers are identifiers declared within a function (or block). Local identifiers are not accessible outside of the function (or block).
Global identifiers are identifiers declared outside of every function definition.
C++ does not allow the nesting of functions. That is, we cannot include the definition of one function in the body of another function.
In general, the following rules apply when an identifier is accessed (p. 334, Malik):
See 3-point discussion at the bottom of page 337 to the middle of page 338
A function that used global variables is not independent and typically cannot be used in more than one program. Also, if more than one function used the same global variable and something goes wrong, it is difficult to discover what went wrong and where. Problems caused by global variables in one area of the program might be misunderstood as problems caused in another area. We are well-advised not to use global variable, but to, instead, use the appropriate (reference) parameters.
The variables discussed so far have followed two simple rules:
A variable for which memory is allocated at block entry and deallocated at block exit is called an automatic variable. A variable for which memory remains allocated as long as the program executes is called a static variable. Global variables are static variables, and by default, variables declared within a block are automatic variables. We can declare a static variable within a block by using the keyword static:
static dataType identifier;
as in:
static int x = 0;
which declares x to be a static variable of the type int and initializes x to 0.
Static variables declared within a block are local to the block, and their scope is the same as that of any other local identifier of that block.
Example 7-10, p. 339
We specify the value of a default parameter when the function name appears for the first time, such as in a prototype. Generally, the following apply for functions with default parameters:
Consider the function:
void funcExp (int x, int y, double t, char z = 'A', int u = 67, char v = 'G', double w = 78.34);
which has 7 parameters: z, u, v, and w.
Page Last Updated: Saturday February 12, 2005 10:21 AM