pTAL Guidelines for TAL Programmers

Coding Guidelines
pTAL Guidelines for TAL Programmers527256-002
2-32
Returning a Value From a Function
Returning a Value From a Function
Guideline: Always specify a function’s return value as part of the RETURN statement.
In TAL, you can explicitly push a function’s value onto the stack and return from the
function using an expressionless RETURN statement, as Example 2-32 on page 2-32.
In pTAL, you must specify a function’s value in a RETURN statement. Also, you cannot
push a value onto the stack because pTAL does not support the STACK statement.
You must convert Example 2-32 on page 2-32 to Example 2-33 on page 2-32 to
compile your program successfully with a native compiler.
RETURN Statement Must Appear in Every Function
Guideline: Include at least one RETURN statement in every function, even if
dynamically no RETURN statement is executed.
TAL does not require you to include a RETURN statement in a function. If a function
arrives at the end of its code, it executes an implicit RETURN statement and returns
zero to its caller.
The native compiler reports a syntax error if a function does not contain at least one
RETURN statement.
Returning From a Function That Does Not Specify a RETURN
Statement
Guideline: Always return from a function by executing an explicit RETURN statement.
In TAL, if a function arrives at the end of its code because it has not executed a
RETURN statement, the function returns 0 to its caller.
Example 2-32. Returning Function’s Value on the Stack (TAL Only)
INT PROC p;
BEGIN
INT i := 0;
STACK i; ! Push value to return onto stack
RETURN;
END
Example 2-33. Returning Function’s Value With RETURN Statement
INT PROC p;
BEGIN
INT i := 0;
RETURN i; ! Return value of i to caller
END;