Examples of Parameter Passing:
void swap(int a, int b)1. The time to bind b and list[i] can be
{
int temp = a;
a = b;
b = temp;
}void main()
{
int i=3;
int list[5]={ 2, 0, 4, 1, 3};
swap(i, list[i]);
}
2. Pass back
from left to right or from right to left.
a. from left to right:
b will copy back to list[1] in the above example.
b. from right to left:
b will bind to list[3] in the above example.
Pass-By-Name:
int global = 3;void fun(int a, int b)
{
global = global + 1;
a = a + 12; ---------> global = global + 12;
b = b - 2; ---------> x = x - 2;
cout<<"a = " << a <<endl;
cout<<"b = " << b <<endl;
cout<<"global = " << global <<endl;
}
main()
{
int x = 5;
fun(global, x);
x = x + 3;
cout<<"After call \n";
cout<<"global = " << global <<endl;
cout<<"x = " << x <<endl;
}
¡@