| Functions / The runtime stack | |
The following data types are passed by reference to a function:
Passing a dynamic array as a function parameter is legal and efficient. When passed as parameter, the runtime system pushes a reference of the dynamic array on the stack, and the receiving local variables in the function can then manipulate the original data.
Returning a dynamic array from a function is also possible: The runtime system pushes the reference of the dynamic array on the stack.
MAIN
  DEFINE arr DYNAMIC ARRAY OF INT
  DISPLAY arr.getLength()
  LET arr = init(10)
  DISPLAY arr.getLength()
  CALL modify(arr)
  DISPLAY arr[50]
  DISPLAY arr[51]
  DISPLAY arr.getLength()
END MAIN
FUNCTION init(c)
  DEFINE c INT
  DEFINE x DYNAMIC ARRAY OF INT
  FOR i=1 TO c 
     LET x[i] = i 
  END FOR
  RETURN x 
END FUNCTION
FUNCTION modify(x)
  DEFINE x DYNAMIC ARRAY OF INT
  LET x[50] = 222
  LET x[51] = 333
END FUNCTION
 Output of the program:
0 10 222 333 51
Like other object oriented programming languages, objects of built-in classes or Java classes are passed by reference. It would not make much sense to pass an object by value, actually. The runtime pushes the reference of the object on the stack (i.e. the object handler is passed by value), and the reference is then popped to the receiving object variable in the function. The function can then be used to manipulate the original object.
MAIN
  DEFINE ch base.Channel 
  LET ch = base.Channel.create()
  CALL open(ch)
  CALL ch.close()
END MAIN
FUNCTION open(x)
  DEFINE x base.Channel -- Channel object reference 
  CALL x.openFile("filename","r")
END FUNCTION
 BYTE or TEXT data types define large data object (LOB) handlers internally implemented as "locators". When you pass a BYTE or TEXT to a function, the locator is pushed on the stack and popped to the receiving BYTE or TEXT variable in the function. The actual LOB data is not copied, only the locator is passed by value.