Returning records from functions

Returning records from functions

In order to return a complete RECORD from a regular FUNCTION, you must put a copy of all record members on the stack, by expanding the record with the .* notation. This is however supported with backward compatibility and not best practice: Consider passing records as reference with the INOUT keyword, or use methods and return the record without using the .* notation.

Returning records from methods

To return a record from a method, define the method with the returned type name in the RETURNS clause, and specify the record variable without using the .* notation in the RETURN instruction of the method body:
TYPE t_rec RECORD
    name VARCHAR(50)
END RECORD

TYPE t_info RECORD
    code INT,
    message STRING
END RECORD

FUNCTION (r t_rec) getInfo() RETURNS t_info
    DEFINE i t_info
    LET i.code = 999
    LET i.message = "xxxxxxx"
    RETURN i  -- WITHOUT .* NOTATION !
END FUNCTION

FUNCTION main()
    DEFINE r t_rec
    DEFINE i t_info
    LET i = r.getInfo()
    DISPLAY i.*
END FUNCTION