Call by Value or Call by Reference :

Call by Value :

When a function is called by the calling program, the values to the arguments in the function are supplied by the calling program. The values supplied can be used inside the function. Any alteration to the value inside the function is not accepted in the calling program but the change is locally avaliable in the function. This method is referred as calling a function by value.

Example :

  1. /* Description: simple pointer use call by Value */
  2. #include<stdio.h>
  3. main()
  4. {
  5. int a = 25,b = 10;
  6. void add(int x, int y);
  7. printf("\n Before function call : a = %d b = %d",a,b);
  8. add(a,b);
  9. printf("\n After function call : a = %d b = %d",a,b);
  10. getch();
  11. }
  12. void add(int x,int y)
  13. {
  14. x = x + 10;
  15. y = y + 10;
  16. printf("\n Inside the function : a = %d b = %d",x,y);
  17. }

Output :

When this program executed, values of the variables a and b are printed before and after the function call and also inside the function as shown below :

c language - simple pointer use call by Value

Call by Reference :

A function can be declared with pointers as its arguments. Such functions are called by the calling program with the address of a variable as argument from it. The address of the variables are substituted to the pointers and any alteration to its value inside the function is automatically carried out in that location. The change is indirectly made and is accepted by the calling program. This method is referred as calling a function by reference.

Example :

  1. /* Description: simple pointer use call by Reference */
  2. #include<stdio.h>
  3. main()
  4. {
  5. int a = 25,b = 10;
  6. void add(int *x, int *y);
  7. printf("\n Before function call : a = %d b = %d",a,b);
  8. add(&a,&b);
  9. printf("\n After function call : a = %d b = %d",a,b);
  10. getch();
  11. }
  12. void add(int x,int y)
  13. {
  14. *x = *x + 10;
  15. *y = *y + 10;
  16. printf("\n Inside the function : a = %d b = %d",*x,*y);
  17. }

Output :

When this program executed, values of the variables a and b are printed before and after the function call and also inside the function as shown below :

c language - simple pointer use call by Reference

Note : That the increase in values of the arguments a and b is available in the main program also.


Comparison between Call by Value and Call by Reference :

Call by ValueCall by Reference
  1. This is used usual method to call a function in which only the value of the variable is passed as an argument.

  2. Memory location occupied by formal and actual arguments is different.

  3. Since a new location is created , this method is slow.
  1. In this method, the address of the variable is passed as an argument.

  2. Memory location occupied by formal and actual arguments is same and there is a saving of memory location.

  3. Since the existing memory location is used through its address, this method is fast.

Next Update

Loading...

We adding new programming language tutorials very soon.

We make a Collaboration with HackIsOn YouTube Channel So, Please Subscribe Their Channel and keep supporting