Which is better, pass, or not pass by reference?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    Which is better, pass, or not pass by reference?


    Folks,

    I've asked this before but never got any response but its important and I
    thought I'd pitch it again (hopefully a bit clearer)...

    One can pass data from one function to another either by passing a copy, or
    passing by reference.

    My understanding of passing by reference (putting the & before a variable
    during the function declaration) means that the original data is used.

    My understanding of *not* passing by reference, (thus passing a copy)
    literally
    means creating a copy of the data contained in my variables - thus doubling
    the memory usage for this data... even if its just a temporary while the
    function is being executed.

    I *believed* (past tense) that one should only pass by reference if they
    wanted the child function to change the data and pass the newer values back
    to the parent function.

    I'm now thinking that this is just a feature and not the only usage. I am
    begining to think I should use "pass by reference" nearly all the time in
    order to conserve memory (since I won't be creating copies of my variable
    data in memory).

    Would this be good practice? Am I right that passing by reference means that
    my variable data is not duplicated thus using less memory than if I had
    passed my variable data as a copy?
    --
    All comments + replies please via the newsgroup,
    Thanks,
    Randell D.
    -
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet?


  • Marcel

    #2
    Re: Which is better, pass, or not pass by reference?


    "Randell D." <you.can.email. me@randelld.at. yahoo.com> schreef in bericht
    news:cU4lb.1380 72$6C4.80562@pd 7tw1no...[color=blue]
    >
    > Folks,
    >
    > I've asked this before but never got any response but its important and I
    > thought I'd pitch it again (hopefully a bit clearer)...
    >
    > One can pass data from one function to another either by passing a copy,[/color]
    or[color=blue]
    > passing by reference.
    >
    > My understanding of passing by reference (putting the & before a variable
    > during the function declaration) means that the original data is used.
    >
    > My understanding of *not* passing by reference, (thus passing a copy)
    > literally
    > means creating a copy of the data contained in my variables - thus[/color]
    doubling[color=blue]
    > the memory usage for this data... even if its just a temporary while the
    > function is being executed.
    >
    > I *believed* (past tense) that one should only pass by reference if they
    > wanted the child function to change the data and pass the newer values[/color]
    back[color=blue]
    > to the parent function.
    >
    > I'm now thinking that this is just a feature and not the only usage. I am
    > begining to think I should use "pass by reference" nearly all the time in
    > order to conserve memory (since I won't be creating copies of my variable
    > data in memory).
    >
    > Would this be good practice? Am I right that passing by reference means[/color]
    that[color=blue]
    > my variable data is not duplicated thus using less memory than if I had
    > passed my variable data as a copy?
    > --
    > All comments + replies please via the newsgroup,
    > Thanks,
    > Randell D.[/color]


    Passing by reference is usefull if you want a function to return more than
    only one value... a function returns normally only one value (although you
    can return an array with multiple results too)...

    Using a reference is done if you want to change a var outside the scope of
    your function....

    Yes, interesting, why not always use call by reference??? Only thing i can
    think is that you do not always want to change values outside the
    function...

    Regards,

    Marcel


    Comment

    • rush

      #3
      Re: Which is better, pass, or not pass by reference?

      "Randell D." <you.can.email. me@randelld.at. yahoo.com> wrote in message
      news:cU4lb.1380 72$6C4.80562@pd 7tw1no...[color=blue]
      > I'm now thinking that this is just a feature and not the only usage. I am
      > begining to think I should use "pass by reference" nearly all the time in
      > order to conserve memory (since I won't be creating copies of my variable
      > data in memory).[/color]

      _if_ I am correct, php 5 will pass by reference by default.

      rush
      --
      Get your very own domain easily. Fast and professional customer service.




      Comment

      • odel

        #4
        Re: Which is better, pass, or not pass by reference?

        Reference is useful only for object.

        if your code is object oriented, then you should always return
        references to your objects, never copies, or you will have some
        surprises (change the object in one place, without changing it somewhere
        else is bad)

        for built-in types like strings, integers, booleans , return copies, you
        will double the use of memory but for this kind of elements, it has
        absolutly NO importance (I'm not even sure you can reference a string
        for example).

        Now even if your code is not OO, you still have some php functions
        returning objects i.e. pg_connect, mysql_result... a connection resource
        is an object, a resultset is an object, then if you manipulate them or
        return them in some functions, return the reference, not the copy.

        another good idea to save your memory is to unset your objects once your
        done with 'em, like one would do in C++ maybe.

        .b

        Comment

        • André Næss

          #5
          Re: Which is better, pass, or not pass by reference?

          Randell D.:
          [color=blue]
          >
          > Folks,
          >
          > I've asked this before but never got any response but its important and I
          > thought I'd pitch it again (hopefully a bit clearer)...
          >
          > One can pass data from one function to another either by passing a copy,
          > or passing by reference.
          >
          > My understanding of passing by reference (putting the & before a variable
          > during the function declaration) means that the original data is used.
          >
          > My understanding of *not* passing by reference, (thus passing a copy)
          > literally
          > means creating a copy of the data contained in my variables - thus
          > doubling the memory usage for this data... even if its just a temporary
          > while the function is being executed.
          >
          > I *believed* (past tense) that one should only pass by reference if they
          > wanted the child function to change the data and pass the newer values
          > back to the parent function.
          >
          > I'm now thinking that this is just a feature and not the only usage. I am
          > begining to think I should use "pass by reference" nearly all the time in
          > order to conserve memory (since I won't be creating copies of my variable
          > data in memory).
          >
          > Would this be good practice? Am I right that passing by reference means
          > that my variable data is not duplicated thus using less memory than if I
          > had passed my variable data as a copy?[/color]

          That's very risky business IMO. At some point you're bound to forget about
          it, and change a variable that was passed by reference. This can lead to
          lots of hard to catch bugs. It is generally accepted that pointers are a
          bad thing, and they should be avoided whenever you can. Functional
          programming languages go farthest in this respect, and this also makes them
          easier to program.

          Also note that the PHP engine has some tricks up it's sleeve to avoid
          unnecessary copying (http://www.zend.com/zend/art/ref-count.php), but I'm
          not sure if this works with argument passing as well.

          All in all I don't think it's worth the hassle. If memory is a problem you
          should instead find the memory hotspots and optimize them, possibly
          rewriting them in C.

          André Næss

          Comment

          • Alan Little

            #6
            Re: Which is better, pass, or not pass by reference?

            Carved in mystic runes upon the very living rock, the last words of odel
            of comp.lang.php make plain:
            [color=blue]
            > Reference is useful only for object.[/color]

            That's not true. Passing by reference is useful if you want the function to
            modify the variable you send to it.
            [color=blue]
            > (I'm not even sure you can reference a string for example).[/color]
            Of course you can.

            --
            Alan Little
            Phorm PHP Form Processor

            Comment

            • Pedro

              #7
              Re: Which is better, pass, or not pass by reference?

              Alan Little didn't write:[color=blue]
              > odel wrote:[/color]

              but Alan Little wrote:[color=blue][color=green]
              >> (I'm not even sure you can reference a string for example).[/color]
              > Of course you can.[/color]

              You can pass all variables by reference.
              What you can't pass by reference are constants:

              <?php
              function xyz(&$p1, &$p2, &$p3) {
              // do nothing
              }

              $s = 'string';
              $i = 7;
              xyz($s, $i, 'ERROR HERE');
              ?>

              --
              I have a spam filter working.
              To mail me include "urkxvq" (with or without the quotes)
              in the subject line, or your mail will be ruthlessly discarded.

              Comment

              Working...