pass by value and reference

Total Post:113

Points:791
C#  Java 
 172  View(s)
Ratings:
Rate this:

What is difference between pass by value and pass by reference?

  1. Post:140

    Points:984
    Re: pass by value and reference

    Pass by value :

           In this we pass copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable.

    example:

    #include<stdio.h>
    int main(){
        int a=5,b=10;
        swap(a,b);
        printf("%d%d",a,b);
        return 0;
    } 
    void swap(int a,int b){
        int temp;
        temp =a;
        a=b;
        b=temp;
    }
    


    Pass by reference :

      In this we pass memory address by a reference its actual variables in function as a parameter. Hence, any modification on parameters inside the function will reflect in the actual variable.

    #incude<stdio.h>
    int main(){
        int a=5,b=10;
        swap(&a,&b);
        printf("%d %d",a,b);
        return 0;
    } 
    void swap(int *a,int *b){
        int *temp;
        *temp =*a;
        *a=*b;
        *b=*temp;
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!