Passing by value/reference?

This is a discussion on Passing by value/reference? within the C++ Programming forums, part of the General Programming Boards category; Okay, so I don't really get this and the way my book explains it just makes it worse. Passing by ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    Passing by value/reference?

    Okay, so I don't really get this and the way my book explains it just makes it worse.

    Passing by value is used if a parameter's data flow is:
    A: one-way, into the function.
    B: one-way, out of the function.
    C: two-way, into and out of the function.
    D: A and B
    E: B and C

    Passing by reference is used if a parameter's data flow is:
    A: one-way, into the function.
    B: one-way, out of the function.
    C: two-way, into and out of the function.
    D: A and B
    E: B and C

    Can someone please explain these to me and how you know the difference?
    And while we are at it whats the difference between int& and int?
    Last edited by Shmamy; 12-11-2011 at 02:53 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    To put it simply, when passing by value, the value of the variable you put in is copied over to the variable in the argument list of the function.
    For a reference, the variables in the argument list of the function becomes aliases to the real variable you pass in.
    int& is a reference to an int.

    To see this in action, make a function that modifies its arguments. Eg:
    Code:
    void foo(int & n) { n = 10; }
    Do that with and without the reference and you will soon realize what happens.
    Hint: print out the value of the variable you call the function with.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Practically speaking, when you additionally consider things for which const-references are suited to, the answer to the second one is actually F (where F is some combination of the others)

    So with your last question, are you saying that you don't actually know what a reference is?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by reference of by value?
    By abd in forum C Programming
    Replies: 32
    Last Post: 01-28-2010, 03:49 PM
  2. Passing by reference.
    By PaulBlay in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2009, 09:04 AM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 12:25 AM
  5. passing value or reference?
    By pari in forum C Programming
    Replies: 3
    Last Post: 11-22-2002, 11:33 AM

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