Thread: What is the difference between....?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    What is the difference between....?

    Hey all,
    Can anyone please explain what is the difference between a pass by reference parameter and a pass by value parameter and why a parameter is important at all?


    Thank you in advance

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Consider this post signed

  3. #3
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    pass by reference is when the parameter references the original value passed into the method (by using the memory location's address)
    pass by value is when a copy of the value is given to the method by parameter instead of a reference to the original. Any modifications done to this parameter in the method don't affect the original value because it becomes just like a local variable.

    You may also want to look up pass by value-reference, and pass by name.
    Last edited by TieFighter; 03-31-2010 at 04:21 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not an array. Pass-by-reference (which is a really bad name) essentially means passing the address of a variable to a function instead of its value. Thus, knowing its memory location, we can modify that memory location and hence the original variable.
    Since all parameters are passed by value (copies are made), any value you pass will be duplicated into a new variable in the new function, hence any changes you make to that variable will not be reflected in the original variable you passed.
    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.

  5. #5
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    My bad - I meant method instead of array. I also don't think you meant to imply all parameters are passed by value.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    TieFighter, I hope you didn't mean "method" anywhere. That's some OO abomination we don't speak here.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by TieFighter View Post
    I also don't think you meant to imply all parameters are passed by value.
    I did, because they are.
    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.

  8. #8
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32

    Exclamation

    Quote Originally Posted by nonoob View Post
    TieFighter, I hope you didn't mean "method" anywhere. That's some OO abomination we don't speak here.
    No, I definitely meant method. This question isn't necessarily about C.
    In C you can pass by reference (int &a) rather than (int a). Even in Java you may pass by reference provided your parameter is an object rather than a primitive type.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TieFighter View Post
    In C you can pass by reference (int &a) rather than (int a).
    YES YOU CAN!

    However, TieFighter, in an anal retentive technical sense (the sense in which say, an exam question might occur): NO YOU CAN'T! When you pass &a, you are still passing a value -- the address of a.

    It is somewhat silly, perhaps to disambiguate C++ references from C pointers, etc. I am sure there are more historical reasons too. Silly, since in any case all languages that permit "pass by reference", somewhere under the hood, are passing by value. So in truth you cannot "pass by reference" on a computer, computer memory is not an abstract realm.

    Point being, I'll second TieFighter and say it is not so unusual to refer to passing by reference in C, but also second Elysia in saying it is not so unusual to hear people observe that this is not truly passing by reference, since the mechanism is transparent (references are opaque).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    Okay... you just defined how pass by reference is implemented. If any professor calls that pass by value, then I'd ask for some of my class money back.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    If a professor calls passing a pointer “pass by value” it simply shows he's familiar with C.

    There is a distinct difference between languages that have actual pass by reference (such as C++) and those that don't (such as C). In C, you have to “do” something with the parameter before you get to the original object. This is as simple as dereferencing, but that is still an action; in C++ no such special action is needed. When you pass by reference, you get the original object automatically. Sure, under the hood a pointer is passed around, but from the user's point of view, they're different. This is the point of saying that C is only pass by value.

    Note: I'm not arguing that calling the C approach pass by reference is incorrect. What I'm saying is that it's not wrong to call the C approach pass by value. You can say “I'm passing a value, and that value is a reference to some other object.” It's an annoyance of terminology; but calling C a strictly pass-by-value language is not incorrect.

    I don't think it'd be an issue at all if all languages had either “true” pass by reference, or none did (necessitating dereferencing)—but there you are.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by TieFighter View Post
    In C you can pass by reference (int &a) rather than (int a). Even in Java you may pass by reference provided your parameter is an object rather than a primitive type.
    Oh dear. Either you're confused with C++ or you complicated that unnecessarily.
    In C++, there is a type T&, but in C, there is only T*.

    Quote Originally Posted by cas View Post
    Sure, under the hood a pointer is passed around, but from the user's point of view, they're different. This is the point of saying that C is only pass by value.
    Actually, here's a thing:
    The C++ standard says that references are aliases and that they do not need to take up any memory. So technically, a reference does not have to be a pointer, although that is exactly how they're implemented in many popular compilers.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM
  5. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 08:32 PM