Passing on parameters

This is a discussion on Passing on parameters within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me how to pass on parameters to a function?...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    19

    Passing on parameters

    Can anyone tell me how to pass on parameters to a function?

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    Code:
    void F(int a)
     
    {
     
       a = 10;
     
    }
     
     
     
    int main(int argc, char* argv[])
     
    {
     
      
     
       int a = 0;
    F(a);
    cout << a << endl;
    }
    a will 0, but not 10. And the book says that it has something to do with passing parameters.

  3. #3
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    you pass the parameter by value - it means any changes to vaariable inside the function are not visible outside.

    If you want to see the changes - you need to pass it by reference
    Code:
    void f(int& val)
    {
       val = 10;
    }
    int main()
    {
       int a = 0;
       f(a);
       std::cout << a << std::endl;
       return 0;
    }
    or just return the result...
    To be or not to be == true

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    Oh, can you just explain to me how the parameters passed by value in the example? The book says that a copy of the parameter has been made. But I don't understand.

  5. #5
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    It is just like it says -
    Code:
    int f(int val)
    {
       val = 10;
    }
    int main()
    {
       int a = 0;
       f(a);
       return 0;
    }
    when main is started the automatic variable a is allocated on stack and its vaue is set to 0
    when f is going to start another automatic varible - I even have given it different name - val is allocated on the stack
    and the value of a is copied to this new location - it is called passing parameters by value.

    all changes that f makes to this new variable do not affect in any way the original variable a.

    So when f is finished - the value of a is left intact
    To be or not to be == true

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    oh. now I understand. Do you have any books on functions?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    I suggest reading a good introductory book on C++, e.g., Accelerated C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    But, is it good to read as many books as possible?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Quote Originally Posted by Allen
    But, is it good to read as many books as possible?
    Yes, if they are good books, but trying to read them simultaneously will probably be problematic for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing functions as parameters?
    By dayalsoap in forum C Programming
    Replies: 8
    Last Post: 08-27-2010, 06:16 PM
  2. Help..having trouble passing parameters
    By paret in forum C Programming
    Replies: 6
    Last Post: 12-02-2009, 07:24 PM
  3. Passing Parameters
    By fry in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 03:06 AM
  4. Passing parameters
    By pdstatha in forum C++ Programming
    Replies: 1
    Last Post: 06-30-2002, 10:07 AM
  5. passing parameters to main
    By jpchand in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21