Featured Post

Monday, October 31, 2016

call by value and call by reference


A function is a reusable piece of code. call by value and call by reference are two methods of passing values from calling  function to called function. 

Under call by value method we pass direct values to a called function from calling function.

/* Swapping of two numbers using call by value of method */

                      void swap(int,int);
                      void main()
                      {
                              int a,b;
                              printf("\n enter any two values");
                              scanf("%d%d",&a.&b);
                              printf("\n Before swapping a=%d\tb=%d",a,b);
                              swap(a,b);
                      }
                      void swap(int c,int d)
                      { 
                         int temp;
                         temp=c;
                         c=d;
                         d=temp;
                         printf("\n After swapping a=%d\tb=%d",c,d);

                      }
OUTPUT:
                  enter any two values
                   5  
                   10
                  Before swapping a=5     b=10
                  After swapping a=10     b=5

note: The parameters used in function call are called actual parameters and the parameters used in function definition are called formal parameters.
In call by value method actual values are not modified and only formal parameter are swapped.



Under call by value method we pass address of formal parameters to a called function from calling function.

/* Swapping of two numbers using call by reference method */

                      void swap(int *,int *);
                      void main()
                      {
                              int a,b;
                              printf("\n enter any two values");
                              scanf("%d%d",&a.&b);
                              printf("\n Before swapping a=%d\tb=%d",a,b);
                              swap(&a,&b);
                      }
                      void swap(int *c,int *d)
                      { 
                         int temp;
                         temp=*c;
                         *c=*d;
                         *d=temp;
                        printf("\n After swapping a=%d\tb=%d",*c,*d);
                      }
OUTPUT:
                  enter any two values
                   5  
                   10
                  Before swapping a=5     b=10
                  After swapping a=10     b=5

Note: In call by reference method we are passing the addressing of original parameters. And in swap function we are swapping the original values at address. So in Call by reference method original values are updated(swapped).

  

C program to find Factorial of a given number using recursive and non recursive functions


                         Functions in c, recursive functions, no argument and no return type, call by value, call by reference, with arguments and with return types, with arguments and no return types and no return type and with arguments , call by value, call by reference, recursive functions, tower of hanoi, gcd using recursion and non recursive function , factorial using recursion and non recursive functions, fibonacci using recursive and non recursive functions.


No comments:

Post a Comment