Question:

What is differant between call by referance and call by value.?

by  |  earlier

0 LIKES UnLike

What is differant between call by referance and call by value.?

 Tags:

   Report

3 ANSWERS


  1. With call by value, the parameter values are copied into the function.  Inside the function, the parameter is a variable that holds a value.  Any changes to the parameter values are lost when the function exits.

    With call by reference, the function is referencing the original variables defined outside the function.  Inside the function, the parameter is an alias for the outside variable.  Any changes to the parameter values immediately affect the outside variables used as parameters to the function call.

    Call by value is simpler, so that's usually the best way to go.  There are two reasons to call by reference:

    1.  If the variable is a large object, call by value automatically copies it in memory, which might take more memory or more processor time than you want.

    2.  If you want the function to return more than one value, or to modify an existing object in some way, call by reference is a natural way to do it.

    In an object oriented language, note that member functions implicitly refer to the instance they belong to by reference.


  2. In call by value method,a compiler get a copy of the variable and thus changes made in the value in function will not reflected back to the called function.

    But in call by reference method,the compiler didn't get any copy ,but actually it works on the original copy and thus changes will be reflected back


  3. call by value is the default way to handle parameters in a function. To handle them the copy operator is used so modifying the parameter won't change the original variable passed to the function.

    function by_value($a, $b){}

    call by reference alters the way parameters are handled. A reference is an alias for the passed variable so changing it will affect the original variable instead of a local copy. Instead of copying a whole lot of data, e.g. objects, call by reference safes a lot of storage due to not copying or, in other words, doubling the merory needed to store the object.

    function by_reference(&$a, &$b){}

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.