Skip to main content
FW version: Stable

Functions

info
  • Code blocks and functions are first uploaded to MCU memory and then executed.
  • Other code is executed line by line.

Functions are essentially just blocks of code with a name, making writing scripts easier because we don't have to write all the commands again and again, we just call the functions and all the commands will be called for us. The command

call [func-name]

calls previously defined functions. If an argument is given, the command will try to call the function with the given name. If no argument is given, the command will call the last defined function. Beware that this holds only for the first call command after a function definition, every next call will print func not found.

info

If you want to pass arguments to the function, you can use the set command to set the global variables before calling the function.

In YOS, we create functions with this syntax: .<func-name> { code }. An example of creating a function called greet:

.greet {
echo hello
echo ahoj
}

We created a function that upon calling will execute commands echo hello and then echo ahoj and end. We can then call it by executing call greet. Beware that functions are always saved only in memory, meaning all functions will be gone after the device is turned off.

Example 1

.func1 {    # define function func1
echo hi im func1
}

.func2 { # define function func2
echo hello im func2
}

call # call the last defined function, meaning func2

call func1 # call func1

Result:

hello im func2
hi im func1

Example 2

.foo {
echo I am foo
var i #local variable, exists until the function returns
set i input*input
set output i
#In this case, the i variable is optional. Alternatively:
#set output input*input
}

.bar {
echo I am bar
var i
set i input+input
set output i
#In this case, the i variable is optional. Alternatively:
#set output input+input
}

.main {
var input output int32 #local variables for our calling convention
if input<5 {
call foo
echo foo returned $output
call bar
echo bar returned $output
set input input+1
branch s-
}
}

echo Start by calling the main:
call main
echo The end!

echo
echo Since the 'output' and 'input' variables don't exist now,
echo calling the foo here will result to errors:
echo
call foo

Result:

Start by calling the main:
I am foo
foo returned 0
I am bar
bar returned 0
I am foo
foo returned 1
I am bar
bar returned 2
I am foo
foo returned 4
I am bar
bar returned 4
I am foo
foo returned 9
I am bar
bar returned 6
I am foo
foo returned 16
I am bar
bar returned 8
The end!

Since the 'output' and 'input' variables don't exist now,
calling the foo here will result to errors:

I am foo
expression 'input*input' evaluating error -4
settable variable output not found