Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Call By Reference in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 41
    Comment on it

    To pass values into the function is done by the two ways

     

    Pass values into the function also known as call by value

     

    Pass its reference also known as call by reference

     

     

     

    In the first approach the value is directly passed into the function

     

    * C Program to swap two numbers using pointers and function. */
    #include <stdio.h>
    void swap(int a,int b);
    int main(){
      int num1=5,num2=10;
      swap(num1,num2);  /* value of num1 and num2 is passed to swap function */
      printf("Number1 = %d\n",num1);
      printf("Number2 = %d",num2);
      return 0;
    }
    void swap(int a,int b){ 
      int temp;
      temp=a;
      a=b;
      b=temp;
    }

     

     

    Actual values are passed into the function for swapping the numbers.

     

    And in the second approach the reference are passed instead of the values into the function as an argument.

     

    * C Program to swap two numbers using pointers and function. */
    #include <stdio.h>
    void swap(int *a,int *b);
    int main(){
      int num1=5,num2=10;
      swap(&num1,&num2);  /* address of num1 and num2 is passed to swap function */
      printf("Number1 = %d\n",num1);
      printf("Number2 = %d",num2);
      return 0;
    }
    void swap(int *a,int *b){ /* pointer a and b points to address of num1 and num2 respectively */
      int temp;
      temp=*a;
      *a=*b;
      *b=temp;
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: