BYREF

Function functionName(byref var1, byref var2$...)

Description:

Variables passed as arguments into functions and subs are passed "by value" by default, which means that a copy of the variable is passed into the function or sub. The value of the variable is not changed in the main program if it is changed in the function. A variable may instead by passed "byref" which means that a reference to the actual variable is passed and a change in the value of this variable in the function or sub changes the value of the variable in the main program. See also: Function, Sub, Functions and Subroutines

Usage:

Each of the parameters in the function and sub in this example use the "byref" specifier. This means that when the value of a and b are changed in the function that the variables used to make the call (x and y) will also be changed to reflect a and b when the function returns. Try stepping through this example in the debugger.

  'now you can pass by reference
  x = 5.3
  y = 7.2
  result$ = formatAndTruncateXandY$(x, y)
  print "x = "; x
  print "y = "; y
  print result$
  'And it works with subroutines too
  wizard$ = "gandalf"
  call capitalize wizard$
  print wizard$
  end

function formatAndTruncateXandY$(byref a, byref b)
  a = int(a)
  b = int(b)
  formatAndTruncateXandY$ = str$(a)+", "+str$(b)
end function

sub capitalize byref word$
  word$ = upper$(left$(word$, 1))+mid$(word$, 2)
end sub

More about pass by reference

Passing by reference is only supported using string and numeric variables as parameters. You can pass a numeric or string literal, or a computed number or string, or even a value from an array, but the values will not come back from the call in any of these cases. Step through the example in the debugger to see how it works!

  'you can also call without variables, but the changes
  'don't come back
  result$ = formatAndTruncateXandY$(7.2, 5.3)
  print result$
  'and it works with subroutines too
  call capitalize "gandalf"
  a$(0) = "snoopy"
  call capitalize a$(0)
  end

function formatAndTruncateXandY$(byref a, byref b)
  a = int(a)
  b = int(b)
  formatAndTruncateXandY$ = str$(a)+", "+str$(b)
end function

sub capitalize byref word$
  word$ = upper$(left$(word$, 1))+mid$(word$, 2)
end sub