Thread: Passing arguments by value

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    Passing arguments by value

    Code:
    #include <iostream>
    using namespace std;
    int add (int i)
    {
    	i=i+100;
    	return i;
    }
    int main()
    {
    int n=1;
    add(n);
    cout << "n:" << n << endl;
    n=add(n);
    cout << n << endl;
    	system("pause");
    	return 0;
    }
    Why is result 1 in statement add(n) and why it is 101 in n=add(n).I have also noticed that if I write
    Code:
    cout << "n:" << add(n) << endl;
    instead
    Code:
     add(n);
    result is 101?

    In which cases it will return 1 and why?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Since the parameter is passed by value, the function cannot modify it. The function modifies a copy, then returns the result. So do this instead:
    Code:
    int n=1;
    n = add(n); // Assign n to what the function returns
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Quote Originally Posted by bithub View Post
    Since the parameter is passed by value, the function cannot modify it. The function modifies a copy, then returns the result. So do this instead:
    Code:
    int n=1;
    n = add(n); // Assign n to what the function returns
    But why I need to assign it to n?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Because the add function isn't modifying the passed value. If you want the add function to modify the value that is passed in, then do this:
    Code:
    int add (int& i)
    {
    	i=i+100;
    	return i;
    }
    This way i is passed by reference instead of by value. Now any changes add makes will change the caller's variable.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int n = 1;
    add(n);
    In the case where the variable is passed by value, the above code will change the local version of the variable n (called i within the function itself) however, since this new value (although returned to the caller) is not assigned back to n in main, it (n) maintains its old value of 1.

    Code:
    int n = 1;
    n=add(n);
    In this case, the returned value 101 is assigned back to n overwritting what was there (a 1). Thus n will now contain 101 after the function call.

    Code:
    int n = 1;
    cout << "n:" << add(n) << endl;
    This will print the value returned by the function (101) but will not update n, n will still remain 1 after the call to add because it hasn't been changed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    I understand it now.

    Moderators can lock this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Arguments
    By bolivartech in forum C Programming
    Replies: 13
    Last Post: 10-15-2009, 01:31 PM
  2. Passing arguments
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 01:14 PM
  3. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  4. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  5. passing arguments
    By Baard in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2003, 08:10 AM