The parameters that appear in a function call statement are actual parameter and the parameter in function definition are called formal parameters.
In call by value method a copy of argument values r created and is then used in the function whereas in call by reference method the original variables are used in function call.
example:
1:)Swap numbers by call by value method:
void main()
{
void swap(int,int);
int a,b;
a=7;
b=4;
cout<<"the original values r";
cout<<"a="<<a<<"b="<<b;
swap(a,b);// invoke swap function 2 interchange a and b
cout<<"the values after swap() r";
cout<<"a="<<a<<"b="<<b;
getch();
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"swapped values r";
cout<<"a="<<x<<"b="<<y;
}
output will b
a=7 b=4
swapped values r a=4 b=7
the values after swap() r a=7 b=4
2:) Swap numbers by call by value method:
void main()
{
void swap(int &,int &);
int a,b;
a=7;
b=4;
cout<<"the original values r";
cout<<"a="<<a<<"b="<<b;
swap(a,b);// invoke swap function 2 interchange a and b
cout<<"the values after swap() r";
cout<<"a="<<a<<"b="<<b;
getch();
}
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"swapped values r";
cout<<"a="<<x<<"b="<<y;
}
output will b
a=7 b=4
swapped values r a=4 b=7
the values after swap() r a=4 b=7 (i.e. a and b are modified)
In call by value method a copy of argument values r created and is then used in the function whereas in call by reference method the original variables are used in function call.
example:
1:)Swap numbers by call by value method:
void main()
{
void swap(int,int);
int a,b;
a=7;
b=4;
cout<<"the original values r";
cout<<"a="<<a<<"b="<<b;
swap(a,b);// invoke swap function 2 interchange a and b
cout<<"the values after swap() r";
cout<<"a="<<a<<"b="<<b;
getch();
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"swapped values r";
cout<<"a="<<x<<"b="<<y;
}
output will b
a=7 b=4
swapped values r a=4 b=7
the values after swap() r a=7 b=4
2:) Swap numbers by call by value method:
void main()
{
void swap(int &,int &);
int a,b;
a=7;
b=4;
cout<<"the original values r";
cout<<"a="<<a<<"b="<<b;
swap(a,b);// invoke swap function 2 interchange a and b
cout<<"the values after swap() r";
cout<<"a="<<a<<"b="<<b;
getch();
}
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"swapped values r";
cout<<"a="<<x<<"b="<<y;
}
output will b
a=7 b=4
swapped values r a=4 b=7
the values after swap() r a=4 b=7 (i.e. a and b are modified)