Runtime stack

The runtime stack is used to pass/return values to/from functions.

When passing arguments to a function or when returning values from a function, you are using the runtime stack. When you call a function, parameters are pushed on the stack; before the function code executes, parameters are popped from the stack in the local variables defined in the function. On the other hand, each parameter returned by a function is pushed on the stack and popped into variables specified in the RETURNING clause of the caller.

Elements are pushed on the stack in a given order, then popped from the stack in the reverse order. This is transparent to the programmer. However, if you want to implement a C extension, you must keep this in mind.

According to the data type, parameters are passed and returned by value or by reference. When an element is passed/returned by value, a complete copy of the value is passed. When an element is passed by reference, only the handle of the object is passed/returned. If the type allows it, elements passed by reference can be manipulated in the called function to modify the value.

Table 1. Function parameter and returning rules by language element type
Mode Data type or data structure
By value BOOLEAN, BIGINT, INTEGER , SMALLINT, TINYINT, FLOAT , SMALLFLOAT, DECIMAL, MONEY, CHAR, VARCHAR, DATE, DATETIME, INTERVAL, records (by default, expanded) and static arrays (cannot be returned).
By reference Dynamic arrays, dictionaries, objects (from Java, built-in or extension classes), BYTE/TEXT, STRING (but cannot be modified), records (in methods, and/or with INOUT)