1- Call by value
int a = 100; // Global variable
void P(int x)
{
a = 1000; // this will update global variable a 100 to 1000.
print(x); // will print value of x which is local to function P 100.
a = a + 20; // expression will be executed as 1000 + 20 and will be stored in a as 1020.
x = x + 10; //expression will be executed and it will update the local variable x as 100 + 10 = 110.
print(x); // will print the local value of x which is 110.
}
void main()
{
P(a); // This will pass global a which is 100.
}
//after completion of program a will have the final value as 1020.
2- Call by reference
int a = 100; // Global variable
void P(int *x)
{
a = 1000; // this will update global variable a 100 to 1000.
print(*x); // will print value of a which is pointed by integer pointer x, 1000.
a = a + 20; // expression will be executed as 1000 + 20 and will be stored in a as 1020.
*x = *x + 10; //expression will be executed and it will update global variable a which is pointed by x as 1020 + 10 = 1030 and 1030 will be updated in global variable a.
print(*x); // will print the value pointed by x which is global variable a 1030.
}
void main()
{
P(&a); // This will pass address of global variable a.
}
//after completion of program a will have the final value as 1030.
3- Call by name
int a = 100; // Global variable
void P(int x) // every x will be replaced by a.
{
a = 1000; // this will update global variable a 100 to 1000.
print(x) print(a); // will print value of global a which is 1000.
a = a + 20; // expression will be executed as 1000 + 20 and will be stored in global a as 1020.
x = x + 10 a = a + 10; //expression will be executed and it will update the global variable a as 1020 + 10 = 1030.
print(x) print(a); // will print the global variable a which is 1030.
}
void main()
{
P(a); // This will pass value of global a which is 100.
}
//after completion of program a will have the final value as 1030.
4- Call by value result
int a = 100; // Global variable
void P(int x)
{
a = 1000; // this will update global variable a 100 to 1000.
print(x); // will print value of x which is local to function P 100.
a = a + 20; // expression will be executed as 1000 + 20 and will be stored in a as 1020.
x = x + 10; //expression will be executed and it will update the local variable x as 100 + 10 = 110.
print(x); // will print the local value of x which is 110.
} //once the method calling will over all formal parameter will be dumped into actual parameter, value of x will be dumped into global a so a value will be replaced from 1020 to 110.
void main()
{
P(a); // This will pass global a which is 100.
}
//after completion of program a will have the final value as 110.
5- Call by need (this is not good example for call by need so I am modifying some lines to understand concept better)
int a = 100; // Global variable
void P(int x) // temp value will be passed in x.
{
a = 1000; // this will update global variable a 100 to 1000.
print(x); // will print value of x and in x temp value is stored, 110.
a = a + 20; // expression will be executed as 1000 + 20 and will be stored in a as 1020.
print(x + 10); // expression will be executed as 110(temp value) + 10 = 120 and will print 120.
}
void main()
{
P(a + 10); // execute the expression as 100 + 10 = 110 and will store in temp variable.
}
//after completion of program a will have the final value as 1020.