Results 1 to 4 of 4

Thread: diff between parameter passing by reference and passing by value

  1. #1
    Junior Member
    Join Date
    Apr 2007
    Answers
    2

    diff between parameter passing by reference and passing by value

    wat is difference between parameter passing by reference and parameter passing by value? and example?


  2. #2
    Moderator
    Join Date
    Sep 2006
    Answers
    920

    Re: diff between parameter passing by reference and passing by value

    Parameters passing by reference means we are directly passing the memory address where value is saved.

    Parameters passing by value means we are passing the parameter value only.

    check this link for a better example.
    http://www.cs.princeton.edu/~lworthi...s_val_ref.html

    Last edited by jainbrijesh; 04-24-2007 at 09:03 AM.
    Regards,
    Brijesh Jain
    ---------------------------------------------------------
    Connect with me on Skype: jainbrijesh
    Google Plus : jainbrijeshji

  3. #3
    Expert Member
    Join Date
    Nov 2006
    Answers
    518

    Re: diff between parameter passing by reference and passing by value

    Parameter passing by value means only a value is passed to the function and any changes to the parameters doesn’t change the original arguments passed to the function.
    Eg

    main()
    {
    int a=10,b=20,c=0;
    c=func(a,b); // pass by value
    print a // prints 10
    print b // prints 20
    };

    func(int x,int y) // recieves values 10 & 20
    {
    x=x+1; // adds 1 to x
    y=y+1; // adds 1 to y
    print x; // prints 11
    print y; // ptints 21
    }

    Parameter passing by reference means the address of the original arguments are passed to the function and any changes to the parameters change the original arguments also.
    Eg

    main()
    {
    int a=10,b=20,c=0;
    c=func(&a,&b); // pass by reference
    print a // prints 11
    print b // ptints 21
    };

    func(int *x,int *y) // recieves references to a & b
    {
    *x=*x+1; // adds 1 to a
    *y=*y+1; // adds 1 to b
    print *x; // prints a as 11
    print *y; // prints b as 21
    }

    Lack of WILL POWER has caused more failure than
    lack of INTELLIGENCE or ABILITY.

    -sutnarcha-

  4. #4
    Contributing Member
    Join Date
    Mar 2007
    Answers
    42

    Re: diff between parameter passing by reference and passing by value

    Excellent example !!! Its good one :-)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact