Function call by reference in C

Function call by reference in C

//Function call by Reference(address).use pointer

#include<stdio.h>

int call(int *);    //function declaration

main()
 {

   int i, y, a=100;

   y=call(&a);     //function call(value of y is which function call return to main function by function definition)

   printf("Value of a:%d",a);    //so value of actual argument change if formal argument change

   printf("\n");

   printf("Value of y:%d",y);     //value of y is which function call return to main function

   printf("\n");

   call(&a);    //again function call

   printf("Value of a:%d",a);     //value of a changed

   printf("\n\n");
 
  for(i=0;i<10;i++)
  {
 
  call(&a);

     //function call until i<10. so loop run 10 times.function call also called 10 times.addres passed instead of value.and every time value of a increase by 200.

   printf("Value of a:%d",a);      //value of a changed.if call by value then value of a unchaged so output of function keep in other variable like y=call(a) then it give exact answer otherwise it print value of a.in call by reference if u print a then it gives right answer not print old value of a.bcuz a chage with formal argument

   printf("\n\n");
  }

 }

int call(int *x)    //function definition.adddress of 'a' in 'x'
 {

  *x=*x+200;

  printf("Value of x:%d",*x);

  printf("\n");

 return *x;
 }


Related Posts