Page 113 - Open Soource Technologies 304.indd
P. 113

Open Source Technologies



                   Notes         you to change it from the calling scope. To return a variable by-reference, you need to define
                                 the function as such by placing an and sign in front of the function’s name and in the caller’s
                                 code, assigning the return value by reference to $value:
                                       Example:                  function and get_global_variable($name)

                                                                     {
                                                                                  return $GLOBALS[$name];
                                                                       }
                                                                       $num = 10;
                                                                       $value =& get_global_variable(“num”);
                                                                       print $value . “\n”;

                                                                       $value = 20;
                                                                       print $num;
                                 The previous code prints as
                                 10

                                 20
                                 You can see that $num was successfully modified by modifying $value, because it is a reference
                                 to the global variable $num.

                                 You won’t need to use this returning method often. When you do, use it with care, because
                                 forgetting to assign by reference the by-reference returned value can lead to bugs that are
                                 difficult to track down.

                                 7.5 Declaring Function Parameters

                                 As previously mentioned, you can pass an arbitrary amount of arguments to a function. There
                                 are two different ways of passing these arguments. The first is the most common, which is called
                                 passing by value, and the second is called passing by reference. Which kind of argument passing
                                 you would like is specified in the function definition itself and not during the function call.
                                 7.5.1 By-Value Parameters

                                 Here, the argument can be any valid expression, the expression is evaluated, and its value is
                                 assigned to the corresponding variable in the function. For example, here, $x is assigned the
                                 value 8 and $y is assigned the value of $c:
                                       Example:                  Function pow($x, $y)

                                                                 {
                                                              ...
                                                                       }

                                                                    pow(2*4, $c);
                                 7.5.2 By-Reference Parameters

                                 Passing  by-reference  requires  the  argument  to  be  a  variable.  Instead  of  the  variable’s  value
                                 being passed, the corresponding variable in the function directly refers to the passed variable




        108                               LOVELY PROFESSIONAL UNIVERSITY
   108   109   110   111   112   113   114   115   116   117   118