Thread: Passing by reference...or by value....

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    16

    Passing by reference...or by value....

    Basically im still learning about the two but im trying to do something. basically i have 2 files. in the first i have
    Code:
    int taco;
    someFunctionCall(taco);
    Then in the someFunctionCall i have
    Code:
    void someFunctionCall(int a)
    {
         do math and conversions and etc here
    }
    and i need to set that variable a to the int taco above, reason being is that i need to have function calls but I cannot return anything, if I could return this would be alot easier thanks for any info!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Zocheyado
    reason being is that i need to have function calls but I cannot return anything, if I could return this would be alot easier
    Why can't you return? Typically, an out parameter would be used because some other value is already being returned, or it is expensive to copy. Neither of these apply here.
    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

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Use pointers.
    Make your function accept the pointer and when calling, pass the address.
    Code:
    void foo (int* x)
    {
        *x +=1;
    }
    //call 
    foo(& int_var);

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    well cannot return because it is my assignment to not do it that way ^^, in terms of my assignment its pretty trivial i just must have missed when he mentioned how to do it =/. he talked about it like it was a pretty common thing in C to change the value of some variable in a different class. but i don't know

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    Quote Originally Posted by manasij7479 View Post
    Use pointers.
    Make your function accept the pointer and when calling, pass the address.
    Code:
    void foo (int* x)
    {
        *x +=1;
    }
    //call 
    foo(& int_var);
    AAhhh that was it, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by value/reference?
    By Shmamy in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2011, 11:53 PM
  2. Passing by reference of by value?
    By abd in forum C Programming
    Replies: 32
    Last Post: 01-28-2010, 04:49 PM
  3. Passing by reference.
    By PaulBlay in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2009, 09:04 AM
  4. passing reference more than once
    By l2u in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2006, 10:03 AM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM