How Can We Help?

Print

Subroutines In/Out. Pass by value or reference

There are two options for transmitting an object to a subroutine:

  • Pass by value, indicated by a single arrow to the right or in, passes a numeric value into the subroutine variable that you define.
    • Pass by value means that a numeric value is passed from the calling routine to this routine.
    • If the numeric value comes from a tagnamed variable in the calling routine, the subroutine cannot affect the value in the variable in the calling program. It just uses the value passed in.
  • Passing by reference, illustrated by the opposing arrows indicating in and out, passes a reference or handle to a variable in the calling routine.
    • Pass by reference means that a “reference” to a data item is passed in.
    • By using a reference that is passed in, data in the calling routine can be accessed and changed.
    • Changing data passed by reference is the equivalent of passing data out.

Full arrays can only be passed by reference. Individual array elements can only be passed by value. If you configure to pass an array element, you must place the array index in square brackets []

In 2 words

Pass by value.
Value is not modified.
It’s not the same value in subroutine and main program.
Pass by reference.
Value is modified.
It’s the same value in subroutine and main program.