Return to Course Outline

Actual argument and Formal parameter:

Value parameter and Reference parameter: Example:    In the following examples, num1 and num2 are actual arguments.
                                                         x and y are formal parameters.
 
#include <iostream.h>
void getData(int &x, int &y);

void main ()
{
 int num1=100, num2=200;
    getData(num1, num2);
 cout<<"num1= "<<num1<<", num2= "<<num2<<endl;

void getData(int &x, int &y)
{
 cout<<"x= "<<x<<", y= "<<y<<endl;
 cout<<"Enter two integers."<<endl;
 cin>>x>>y;
 cout<<"x= "<<x<<", y= "<<y<<endl;
}

#include <iostream.h>
void getData(int x, int y);

void main ()
{
 int num1=100, num2=200;
    getData(num1, num2);
 cout<<"num1= "<<num1<<", num2= "<<num2<<endl;

void getData(int x, int y)
{
 cout<<"x= "<<x<<", y= "<<y<<endl;
 cout<<"Enter two integers."<<endl;
 cin>>x>>y;
 cout<<"x= "<<x<<", y= "<<y<<endl;
}

Pass by reference:
num1 and x use the same box, 
num2 and y use the same box.
Pass by value, num1 and x are different boxes.
when main()calls getaDat(), the value of num1 is copied to x and the value of num2 is copied to y.
But, when you finish executing getData(), and go back to main(), the value of x is not copied back to num1, similar to y and num2.
num1 and num2 are local variables for main()
In the getData(), you can not reference num1 and num2
i.e. You can not use them in the getData().
That is, the scope of num1 and num2 is main() only.
Similarly, the scope of x and y is getData() only.

See an additional example