Returning values in functions

Returning data from a function is a simple process - just place the data to be returned to the caller directly after the endfunction keyword, as shown in this example.

do
	print ( myFunction ( 10, 20 ) )
	
sync ( ) loop
function myFunction ( a as integer, b as integer ) c as integer = 0 c = a + b endfunction c

The variable c is declared after the keyword endfunction, resulting in the value of c being returned to the caller. The code within the function declares c as an integer and assigns it a default value of 0. The next line performs an expression to add the values of a and b and assign the result to c.

Inside the do loop, when the print command is called it passes in a function call to myFunction as a parameter. This is valid because when myFunction has run its return value passed as a parameter to the print command.

Return values can either be a variable, an expression or a literal value, as shown in this example.

do
	print ( myFunctionA ( 10, 20 ) )
	print ( myFunctionB ( 10, 20 ) )
	print ( myFunctionC ( 10, 20 ) )
	print ( myFunctionD ( 10, 20 ) )
	
sync ( ) loop
function myFunctionA ( a as integer, b as integer ) c as integer = 0 c = a + b endfunction c
function myFunctionB ( a as integer, b as integer ) c = a + b endfunction c
function myFunctionC ( a as integer, b as integer )
endfunction a + b
function myFunctionD ( a as integer, b as integer )
endfunction 30

myFunctionA declares a variable c, and assigns it the value of a + b. The value of c is returned once the function has completed.

myFunctionB declares a variable c, which is not explicitly declared, meaning it is treated as an integer, which is assigned the value of a + b. The value of c is returned once the function has completed.

myFunctionC has nothing happening inside the function. Instead the return value is calculated directly after the endfunction keyword, where the values of a and b are added together and returned to the caller.

myFunctionD returns a literal value to the calling function (30).