|
|
|
|
|
|
|
parameters. (Some programmers use the terms actual argument and formal argument instead of actual parameter and formal parameter. Others use the term argument in place of actual parameter, and parameter in place of formal parameter.) |
|
|
|
 |
|
 |
|
|
Formal Parameter A variable declared in a function heading. |
|
|
|
 |
|
 |
|
|
Actual Parameter A variable or expression listed in a call to a function. |
|
|
|
|
|
|
|
|
In the NewWelcome program, the actual parameters in the two function calls are the constants 2 and 4, and the formal parameter in the PrintLines function is named numLines. The main function first calls PrintLines with an actual parameter of 2. When control is turned over to PrintLines, the formal parameter numLines is initialized to 2. Within PrintLInes, the countcontrolled loop executes twice and the function returns. The second time PrintLines is called, the formal parameter numLines is initialized to the value of the actual parameter, 4. The loop executes four times, after which the function returns. |
|
|
|
|
|
|
|
|
Although there is no benefit in doing so, we could write the main function this way: |
|
|
|
|
|
|
|
|
int main()
{
int lineCount;
lineCount = 2;
PrintLines(lineCount);
cout << Welcome Home! << endl;
lineCount = 4;
PrintLines(lineCount);
return 0;
} |
|
|
|
|
|
|
|
|
In this version, the actual parameter in each call to PrintLines is a variable rather than a constant. Each time main calls PrintLines, a copy of the actual parameter's value is passed to the function to initialize the formal parameter numLines. This version shows that when you pass a variable as an actual parameter, the actual and formal parameters can have different names. |
|
|
|
|
|
|
|
|
The NewWelcome program brings up a second major reason for using functionsnamely, a function can be called from many places in the main function (or from other functions). Use of multiple calls can save a great |
|
|
|
|
|