Tutorial topicsTutorial home
ELC language reference
Commenting your code
ELC app structure
An empty ELC program
Hello world
Functions
Defining variables
Function parameters
Using variables
Function return values
IF decisions
WHILE loops
FOR loops
Arrays of variables
Library functions
Game Tech - Depth & 3D
Game Tech - Shooting bullets
Dev Tech - How to debug
|
Returning values from functions
Functions sometimes need to return values, either numbers or words. The easiest way to do this is to set the value of a
global variable from within a function. After the function completes, the global variable can be read by the code that had
called the function to retrieve the information that was returned.
As well as setting any global variable to return a value from a function, you can instead use one of the special global
variables set up by the YOUSRC system if you wish. The YOUSRC system sets up the global variables called RETVAL (short
for RETurn VALue) for storing numbers, and RETVAL$ for storing words, and uses these to return information to you from
library functions. The details under the ELC functions tab on the left had side of this web site tell you exactly what
each function returns using RETVAL and RETVAL$.
The best way to use these it to look in the library function reference to see what a function does to RETVAL or RETVAL$
(for example UTIL.RANDOM(5) whould store a random value between 0 and 5 in RETVAL) and then copy the value of RETVAL
to one of your local or global variables on the next line of code using a LET instruction.
One thing to take care of is if you don't use RETVAL after calling a library function, and don't copy it to another
local or global variable then the next call to another library function could change RETVAL or RETVAL again depending
on what that function does. As a student called Iszy told her class when they were discussing RETVAL... "So RETVAL
is like a doorman who can only remember the last thing to pass through the door and variables are like different elves
that come and ask him what the last thing he remembered is".
Here's an example where we choose to use RETVAL and RETVAL$...
// ===================================================
// This ELC app uses a function to add two numbers,
// and another to combine two words together. In each
// case the result is displayed on the screen.
FUNCTION START()
{
AddTwoNumbers(2,5)
GFX.WRITELINE(RETVAL)
CombineTwoWords("Hello","World")
GFX.WRITELINE(RETVAL$)
}
// ===================================================
// Simple function that adds two numbers together
FUNCTION AddTwoNumbers(Num1,Num2)
{
LET RETVAL = Num1 + Num2
}
// ===================================================
// Simple function that combines two words with a
// space between them
FUNCTION CombineTwoWords(Word1$,Word2$)
{
LET RETVAL$ = Word1$ + " "
LET RETVAL$ = RETVAL$ + Word2$
}
|
|
Here the START function calls each function in turn, passing some parameters, and displaying the result
on a line of its own.
You can find this app in the Examples area as an app called
"15 - Returning values from functions".
Here's an example where we choose to use our own global variables instead of RETVAL and RETVAL$...
// ===================================================
// This ELC app uses a function to add two numbers,
// and another to combine two words together. In each
// case the result is displayed on the screen.
GLOBAL Sum,Words$
FUNCTION START()
{
AddTwoNumbers(2,5)
GFX.WRITELINE(Sum)
CombineTwoWords("Hello","World")
GFX.WRITELINE(Words$)
}
// ===================================================
// Simple function that adds two numbers together
FUNCTION AddTwoNumbers(Num1,Num2)
{
LET Sum = Num1 + Num2
}
// ===================================================
// Simple function that combines two words with a
// space between them
FUNCTION CombineTwoWords(Word1$,Word2$)
{
LET Words$ = Word1$ + " "
LET Words$ = Words$ + Word2$
}
|
|
After declaring the global variables, the START function calls each function in turn, passing some parameters, and
displaying the result on a line of its own.
You can find this app in the Examples area as an app called
"24 - Returning values from functions #2".
Next, making decisions with IF...

|