Returning values from functions

When you return values from a function with the RETURN clause, the values are pushed on the stack and popped to the variables specified in the RETURNING clause.

Simple data types that are passed by value are returned by value following the same principle:

MAIN
  DEFINE x INTEGER
  LET x = int_add(10,20)
END MAIN

FUNCTION int_add(n1,n2)
  DEFINE n1, n2 INTEGER
  RETURN (n1+n2)
END FUNCTION

Complex data types are not returned by value, only the reference is pushed on the stack. You can, for example, create an object or a dynamic array in a function and return it to the caller for usage:

MAIN
  DEFINE arr DYNAMIC ARRAY OF INTEGER
  LET arr = create_array(10)
  DISPLAY arr.getLength()
END MAIN

FUNCTION create_array(n)
  DEFINE n, i INTEGER
  DEFINE arr DYNAMIC ARRAY OF INTEGER
  FOR i=1 TO n
     LET arr[i] = i
  END FOR
  RETURN arr
END FUNCTION