A variable declared in the heading of a function is called a parameter, also called a formal argument or a formal parameter. A variable or expression included in the call to a function is referred to as an argument, also known as an actual argument or actual parameter. Please read the following for further information:
// Program starsNum prints num_stars on the screen.
#include <iostream>
using namespace std;
// printStars prints num_stars stars on the screen.
void printStars(int); //Function prototype, with one integer parameter
int main ()
{
int num_stars;
cout << "How many stars do you want to print out? Enter an integer: ";
cin >> num_stars;
cout << endl;
cout << "The next line contains " << num_stars << " stars. " << endl;
printStars(num_stars); //Function call
return 0;
}
//**********************************************************************
//Function definition, with one integer parameter
//**********************************************************************
// Post: numofstars asterisks are sent to cout.
void printStars (int numOfStars)
{
int i;
i = 0;
while (i < numOfStars)
{
cout << "*";
i++;
}
// Here is a for loop which does the same thing
// that the above while loop does.
//
// for (i=0; i < numOfStars; i++)
// {
// cout << "*";
// }
cout << endl;
return;
}
In the above example, the heading of printStars()
function is
void printStars(int numOfStars)
.
The parameter numOfStars
is a value
parameter because its data type int
does not end with an ampersand ( &
).
When it is called using the argument num_stars
, i.e. printStars(num_stars);
, then parameter numOfStars
receives a copy of the value of
num_stars
.
While the function runs there are two copies of the data: one in the argument
num_stars
and one in the parameter
numOfStars
. If a statement inside the
function printStars()
were to change
the value of numOfStars
, this change
would not affect the argument num_stars
.
© Department of Computer Science, University of Regina.