recategorized by
1,326 views

1 Answer

2 votes
2 votes

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.

 

 

Related questions

0 votes
0 votes
0 answers
1
Rahul_Rathod_ asked Oct 3, 2018
528 views
from below list of parameter passing techniques, which parameter passing technique we can implement in c?1) call by value2) call by refference3) call by value result4) ca...
0 votes
0 votes
0 answers
2
Rishabh Baghel asked Mar 5, 2018
571 views
Does C support all passing parameters techniques like call by text, call by copy restore, and call by name?
0 votes
0 votes
1 answer
4
Arun Rout asked Jan 7, 2019
729 views
#include<stdio.h>int fun(int arr[]){ arr=arr+1; printf("%d",arr[0]);}int main(void){ int arr ={10,20}; fun(arr); printf("%d",arr[0]); return 0;}A.COMPIL...