8 Replies Latest reply: May 22, 2008 9:26 AM by Vibha Parmar RSS

call by value and call by reference

swamy katta
Currently Being Moderated

Hello all

 

can anybody tell about call by value and call by reference with examples

  • Re: call by value and call by reference
    Prasanth Kasturi
    Currently Being Moderated

    hi

     

     

     

    When you pass a parameter by reference, new memory is not allocated for the value. Instead, a pointer to the original memory location is passed. All references to the parameter are references to the original memory location. Changes to the variable within the subroutine update the original memory location immediately.

     

    example   : data f1 value 'A'.

     

                    perform s1 using f1.

                   write / f1.

     

                   form s1 using p1.

                   p1 = 'X'.

                   endform.

     

    O/P -


    >X

     

     

    When you pass a parameter by value, new memory is allocated for the value. This memory is allocated when the subroutine is called and is freed when the subroutine returns. Therefore, references to the parameter are thus references to a unique memory area that is known only within the subroutine; the original memory location is separate

     

    data: f1 value 'A'.

        perform s1 using f1.

         write / f1.

     

        form s1 using value(p1).

         p1 = 'X'.

          write / p1.

         endform.

     

    O/P----


    >X

                     A

     

     

    regards

    prasanth

  • Re: call by value and call by reference
    Jagadish Avidi
    Currently Being Moderated

    Hi swamy,

     

    Call By Value:

    Creates a new memory loaction for use within the

    subroutine.The memory is freed once it leaves the

    subroutine.Changes made to the variable are not affected

    outside the subroutine.

    Call By Reference:

    Passes a pointer to the memory location.Changes made to the

    variable within the subroutine affects the variable outside

    the subroutine.

     

     

    A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.

     

    A way of passing the address[passing by pointer, reference] of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed.

     

     

     

    Regards,

     

    Jagadish

  • Re: call by value and call by reference
    bhanu malik
    Currently Being Moderated

    Call By Value:

    Creates a new memory loaction for use within the

    subroutine.The memory is freed once it leaves the

    subroutine.Changes made to the variable are not affected

    outside the subroutine.

    Call By Reference:

    Passes a pointer to the memory location.Changes made to the

    variable within the subroutine affects the variable outside

    the subroutine.

     

     

    IN CALL BY VALUE, BOTH THE ACTUAL AND FORMAL PARAMETERS

    WILL BE CREATED IN DIFFERENT MEMORY LOCATIONS WHEREAS IF

    THEY ARE CALLED BY REFERENCE BOTH WILL BE CREATED AT THE

    SAME LOCATION.

  • Re: call by value and call by reference
    swetha r
    Currently Being Moderated

    Hi,

     

    Calling by reference: During a subroutine call, only the address of the actual parameter is transferred to the formal parameters.  The formal parameter has no memory of its own, and we work with the field of the calling program within the subroutine.  If we change the formal parameter, the field contents in the calling program also changes.

     

    ·            Calling by value: During a subroutine call, the formal parameters are created as copies of the actual parameters.  The formal parameters have memory of their own.  Changes to the formal parameters have no effect on the actual parameters.

     

    ·            Calling by value and result: During a subroutine call, the formal parameters are created as copies of the actual parameters.  The formal parameters have their own memory space.  Changes to the formal parameters are copied to the actual parameters at the end of the subroutine.

     

    Reward if useful.

    Regards,

    Swetha.

     

  • Re: call by value and call by reference
    Amit Gujargoud
    Currently Being Moderated

    Hi Swamy,

     

    Please try to search with call by value and call by reference throwing the thread in SDN.Am sure you will got plenty of notes on that.

     

    Please close this thread.

     

    Amit.

  • Re: call by value and call by reference
    Currently Being Moderated

    hi,

     

    Call by Value : Allocates a new memory location for use within the subroutine. The memory is freed when the subroutine ends.

    When you pass a parameter by value, new memory is allocated for the value. This memory is allocated when the subroutine is called and is freed when the subroutine returns. Therefore, references to the parameter are thus references to a unique memory area that is known only within the subroutine; the original memory location is separate. The original is unchanged if you change the value of the parameter.

     

    Advantage : Prevents changes to passed variable.

    example :

    data: f1 value 'A'.

    perform s1 using f1.

    write / f1.

    form s1 using value(p1).

    p1 = 'X'.

    write / p1.

    endform.

    output : X

                A

    u2022Line 1 allocates memory for variable f1.

    u2022Line 2 transfers control to line 7.

    u2022Line 4 causes f1 to be passed by value. Therefore, p1 refers to a new memory location that is independent of f1. The value of f1 is automatically copied into the memory for p1.

    u2022Line 5 modifies the memory for p1. f1 is unchanged.

    u2022Line 6 writes out the value X.

    u2022Line 7 returns control to line 3.

    u2022Line 3 writes out the value A.

    Use pass by value when you need a local copy of a variable that you can change without affecting the original. Pass by reference is more efficient than pass by value.

     

    Call by reference :  Passes a pointer to the original memory location.

    When you pass a parameter by reference, new memory is not allocated for the value. Instead, a pointer to the original memory location is passed. All references to the parameter are references to the original memory location. Changes to the variable within the subroutine update the original memory location immediately.

     

    example :

      data f1 value 'A'.

     

      perform s1 using f1.

    write / f1.

     

    form s1 using p1.

    p1 = 'X'.

    endform.

    output : X.

     

    pls reward if helpful.

  • Re: call by value and call by reference
    Bhaskar Chikine
    Currently Being Moderated

    Hi,

     

    By Value

     

         Data : a type I value 20.

         Perform sub1 using a.

         Write a.

         FORM sub1 using value (p_a)

              P u2013 a = 15

         ENDORM.

    In this case during subroutine call, the formal parameter are created as copies of actual parameter.

    The formal parameters have the memory of their own. Changes made to formal parameter have no effect on the actual parameter.

     

    Like in this case, though value of p_a is changed to 15, it has no effect on u2018au2019 which remains as 20.

     

    By Reference

     

         Data: a type I value 20.

         Perform sub1 using a.

         Write a.

         FORM sub1 using value (p_a)

              P u2013 a = 15.

         ENDORM.

    By default system calls all the forms by reference.

     

    In this case, only the address of the actual parameter is transferred to the formal parameters.  The formal parameter has no memory of its own.  If you change the formal parameter, change is visible in actual parameter also.

     

    Regards,

    Bhaskar

  • Re: call by value and call by reference
    Vibha Parmar
    Currently Being Moderated

    By reference -  Passes a pointer to the original memory location.  Very efficient.

     

    By value -  Allocates a new memory location for use within the subroutine. The memory is freed when the subroutine ends.  Prevents changes to passed variable

     

    Passing Parameters by Reference

    When you pass a parameter by reference, new memory is not allocated for the value. Instead, a pointer to the original memory location is passed. All references to the parameter are references to the original memory location. Changes to the variable within the subroutine update the original memory location immediately.

     

    report zpassbyreference .

    data f1 value 'A'.

     

      perform s1 using f1.

      write / f1.

     

      form s1 using p1.

          p1 = 'X'.

      endform.

     

    The additions using f1 and changing f1 both pass f1 by reference-they are identical in function. The reason they both exist is that-used properly-they can document whether the subroutine will change a parameter or not.

     

    Code changing with parameters, the subroutine changes. You should code using with parameters that are not changed by the subroutine.

     

    report zpassbyreference1.

      data: f1 value 'A',

              f2 value 'B'.

     

      write: / f1, f2.

      perform s1 using f1

                 changing f2.

      write: / f1, f2.

     

    form s1 using p1

             changing p2.

         p1 = p2 = 'X'.

    endform.

     

     

    f1 and f2 are both passed by reference to s1. Therefore, p1 and p2 are pointers to f1 and f2. Changes to p1 and p2 are immediately reflected in f1 and f2. This example only illustrates that it is possible to change any parameter that is passed by reference. You should code your parameters so that using and changing properly document your usage of those parameters.

     

    Passing Parameters by Value

    When you pass a parameter by value, new memory is allocated for the value. This memory is allocated when the subroutine is called and is freed when the subroutine returns. Therefore, references to the parameter are thus references to a unique memory area that is known only within the subroutine; the original memory location is separate. The original is unchanged if you change the value of the parameter.

     

      report zpassbyvalue.

      data: f1 value 'A'.

     

      perform s1 using f1.

      write / f1.

     

      form s1 using value(p1).

          p1 = 'X'.

          write / p1.

      endform.

     

    Use pass by value when you need a local copy of a variable that you can change without affecting the original. Pass by reference is more efficient than pass by value. Use pass by reference unless you need an independent local copy of the variable.

     

    I hope it helps.

     

    Best Regards,

    Vibha

     

    Please mark all the helpful answers

Actions