-
Notifications
You must be signed in to change notification settings - Fork 1
Functions
Functions are reusable code. You can define a function in SIMAS and reuse it over and over.
To define a function, you need a function name, and the number of parameters you want to take.
Let's start with a function with no parameters:
fun myFunction 0;
printc myFunction is called!;
ret;
end fun;
to call this function, do:
call myFunction;
Warning
Note that you can write an infinite loop function by removing the ret; at the end of the function, but if you don't want an infinite loops, keep the ret;
Arguments are simple. The below function takes three arguments:
fun sq 1;
set num result 0;
@ $0 is the first argument, and $1 is the second, and so on;
add num result $0;
mul num result result;
print result;
ret;
end fun;
To call the function:
call sq c 10;
@ or;
set num variable 10;
call sq v variable;
@ either way works;
The c specify that it is a num constant. Alternatively, you can put b for boolean constants, l for list names and v for variable names. Although in this specific use case, only c and v is appropriate.
Example usage of v:
set num myNumber 3;
call addThree c 10 c 20 v myNumber;
Return values are automatically assigned to a variable with the function name and a $ prefix. For example, the return value of addThree will be stored in a variable called $addThree
Copyright (c) 2024-2025 Turrnut
