Tinderbox v9 Icon

Returning function values

Important note: all examples follow aTbRef naming conventions.


Returning function values

return

The return operator is only used inside a function, and if used is used only once per function operator call (though note the conditional branching example below. The return statement returns a value from a function to the calling action code object (which can even be another function). Execution of the functions code stops once an expression containing a return (i.e. up to the next semi-colon) is processed.

Thus, when return is inserted in a function, the code/expression to the end of the line (or first semi-colon) is evaluated and returned. Note that as the return statement is closed by a semicolon, it is essentially a single line of code/expression started by the word 'return' (case-sensitive), a space, and then following action code code. For example:

return "Process complete"; returns a single literal string

return true; returns a boolean value

return $MyNumber; returns an attribute's value

return vMyValue; returns a variable's value

However, if the to-be-returned value is not coded a single term, but rather as an expression whose output is returned, it may be enclosed in parentheses but does not require to be. so The use of parentheses helps indicate, if only to the user, that some evaluation is required before the single result is used as the return value

The first example shows an evaluated result without parentheses, the other with parentheses:

return 2 + 2; 

return ("Hello " + vName"); 

return (iPrice * $Tax); 

Can return occur anywhere in the function?

In theory Yes, but in practice, No. This is because the function process completes after the return value is evaluated and passed back out. So extra code may occur after the return but is not evaluated.

One possible use for content in a function after a return statement is to add comments about a function placed at the end, after code so available for user reference but not pushing the actual code down the (viewable) screen so aiding usability.

Can there be a multiple value return?

If it is desired to pass multiple discrete values in the same return event, make the returned object a list or dictionary (or a variable of List, Set or Dictionary type).

Can there be more than one return call in a function?

Yes, if each return call is in a separate branch of the function code. For instance:

	function fTest(iString){
		if(iString){
			if(iString.size<=5){
				return "Small string";
			}else{
				return "Large string";
		}
		return "No string";
	}

In this case, there are three discrete possible return values but each closes and returns for a different single branch of the code. The logic could as easily set a single variable in each branch and then use a single return at the end of the function. If using multiple return statements, take care to avoid exiting the function too early.


Next: Calling functions