Skip to content

Functions

turrnut edited this page Jul 10, 2025 · 7 revisions

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

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

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

SIMAS Logo

Clone this wiki locally