Skip to content

Functions

turrnut edited this page Dec 31, 2024 · 2 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 addThree 3;
  set num result 0;
  @ $0 is the first argument, and $1 is the second, and so on;
  add num result $0;
  add num result $1;
  add num result $2;
  print result;
  ret;
end fun;

To call the function:

call addThree c 10 c 20 c 30;

The c specify that it is a num or str constant. Alternatively, you can put b for boolean constants 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