Thread: Your ("Pass-By-Ref") Opinion Please

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Your ("Pass-By-Ref") Opinion Please

    Hi. I read something interesting in Bjarne Stroustroup's book. He discourages using functions that modify call-by-reference arguments. He claims that they make programs hard to read.

    I was surprised by this. I didn't learn this, and always learned that to modify a variable one had to use reference arguments. Passing-by-value provided one with a copy (I assume this involves the Copy Constructor), which does not persist.

    Later Mr. Stroustroup declares, "Be suspicious of non-const reference arguments; if you want the function to modify its arguments, use pointers and value return instead."

    I would like to hear some opinions about this. Namely, do you feel that these make programs hard to read, and are they best avoided? Also, how would one "use pointers and value return instead," to accomplish this objective?

    Thanks for the feedback. Steve

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Search the board, it has been discussed numerious times.

  3. #3
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Everybody has an opinion. It's best to research the matter and formulate your own rather than collect everyone else's. You get more out of it that way.
    Kampai!

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Well java doesn't allow passing by reference, so I guess the C++ creator and Sun saw something wrong with passing by reference. I created an example in C to show yall that I believe passing by reference is OK only if the functions are privately used by the specific program, but if the function is used a lot, I believe it's best for compatability to pass by value. Also, I find myself trapped with a memory leak when passing by value using a pointer (I move the pointer away from malloc'ed material):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // Change the string in reference
    void changeString1 (char **s);
    
    // Change the string through value & return
    char *changeString2 (const char *s);
    
    int main (void)
    {
      // Create a string you want to change in a function
      char *s = malloc (255 * sizeof (*s));
    
      // Change string by reference
      changeString1 (&s);
      puts (s);
    
      // Change string by value
      strcpy (s, changeString2 (s));
      puts (s);
    
      return 0;
    }
    
    // Change the string in reference
    void changeString1 (char **s)        // The string that we use to find what we return
    {
      // Change the string
      strcpy ((*s), "Birds sing");
    }
    
    // Change the string through value & return
    char *changeString2 (const char *s)   // The string that we use to find what we return
    {
      // Create a string to return
      char *sc = malloc (255 * sizeof (*sc));
    
      // Use the input value to discover what to set the return string as
      if (!strcmp (s, "Birds sing"))
        strcpy (sc, "Yes that's correct");
      else
        strcpy (sc, "Birds sing");
      return sc;
    }

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    C does not have pass by reference. It only has pass by value.

  6. #6
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >Well java doesn't allow passing by reference
    Sure it does. Any class object in Java is passed by reference.
    Kampai!

  7. #7
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Well, as a beginner-to-intermediate student, I was surprised to read this. I guess because I am moving into an intermediate level in my studies, I am coming across this. It doesn't seem to me (of course, I could be assuming) that beginner-level courses and books state this. I just completed a second-level C++ course in a science-oriented college. The professor wanted us to use Stroustroup's book. I tried to use it as a reference, but was overwhelmed! Later, a friend of mine who is a brilliant Applied Mathematician, recommended that I "read the book a few times, you'll know C++ pretty well by then." I don't know C. I did a search for "pass by reference" and came up with a lot! Alas, I didn't feel that my specific question was answered though. OK, thanks again. Steve

  8. #8
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    OK, here's my attempt at "using pointers and value return:"
    Code:
    #include<iostream>
    
    using namespace std;
    
    int f(int* i)
    {
        *i=(*i)+=40;
        return *i;
    }
    
    int main()
    {
      int j=90;
      int* i = &j;
      cout<<f(i)<<endl;
      cout<<*i<<endl;
      
      cin.get();
      return 0;
    }
    ~Steve

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>*i=(*i)+=40;
    I believe that's undefined behaviour. I think what you want is:
    Code:
    (*i)+=40;
    Other than that, it looks good to me.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Ehh... Back on the original topic, it can make code hard to read if it isn't used carefully (i.e. with good design principles in mind). For example, it is probably unwise to pass by reference instead of returning a value when there is no good reason not to do the latter. That said, there are many good uses. One project I worked on took a result object (something to return the success or nature of the failure of an operation) passed by reference as the last parameter to each function (within the particular subcomponents of code where the result was of use). There was no ambiguity reading the code, as this was expected throughout the code.

    Personally, I prefer not to pass by pointer unless there is a compelling reason to (allowing instances of a derived class, the object could be non-existent [null], etc).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Thantos
    C does not have pass by reference. It only has pass by value.
    Quote Originally Posted by Sake
    >Well java doesn't allow passing by reference
    Sure it does. Any class object in Java is passed by reference.
    omg this is the 500th time I've lied today.

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> Later Mr. Stroustroup declares, "Be suspicious of non-const reference arguments; if you want the function to modify its arguments, use pointers and value return instead." <<

    I agree with Stroustroup. It makes code harder to read. Reading C++ code, it is not unusual to be surprised that a variable has been altered and then have to search up the code to find the culprit function.
    Code:
    doSomething(myInt); /* Can this function alter myInt? */
    doSomething(&myInt); /* I know this function can alter myInt. */
    C# handles this by requiring that the caller must use the ref keyword for pass by reference arguments:
    Code:
    doSomething(ref myInt);

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Function names can be formulated so it's obvious that they'll change the variable. But often it's better to make them member functions instead, if possible.
    For me, it's not that common to have functions modify their arguments. Member functions are better.

    And no, Java does not have 'pass by reference'. What Java does is passing references (=pointers) by value.
    Last edited by Sang-drax; 01-27-2005 at 06:00 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by Sake
    >Well java doesn't allow passing by reference
    Sure it does. Any class object in Java is passed by reference.
    Actually it doesn't. In java you work on object pointers and pass them around by value.

    In almost every situation this will work out exactly the same but there are some situations where the results are different.

    (damn, should learn to read threads till the very end before posting)

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Reading C++ code, it is not unusual to be surprised that a variable has been altered and then have to search up the code to find the culprit function.

    That's true, but if you're careful and thoughtful about how you do it, then there should be no problem. Take Zach's example: The reference argument is, throughout the project, the last argument in every function. There is therefore no ambiguity, since it becomes the norm rather than the exception.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-27-2009, 04:43 PM
  2. Programming Opinion
    By tofugirl in forum C Programming
    Replies: 22
    Last Post: 11-25-2005, 09:15 AM
  3. Her opinion, your opinion
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2002, 10:50 AM
  4. Freedom of opinion
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-10-2002, 07:06 AM
  5. opinion about books
    By clement in forum C Programming
    Replies: 7
    Last Post: 09-24-2001, 04:18 PM