Next: , Previous: Files, Up: Language


2.7 Functions

Functions are defined and called using the following syntax:

# Defining them
{stdout "Hello" ! write;} = func;
{/x,y/stdout "x=" x ", y=" y!write;} = coorprint;
{stdout args ! write;} = aprint;

# Calling them
!func;                 # Outputs "Hello"
52 12 ! coorprint;     # Outputs "x=52, y=12"
999 "abc" 21 ! aprint; # Outputs "999 abc 21"

Arguments can thus be accessed either by using the /.../ syntax in the beginning of the function or by using the local args variable. Furthermore, to make all new variables save in the global space, prepend the function content with a *. This will have the same effect as replacing all = signs in the function with the * operator.

Remember that your file is also a function, just without the curly brackets. This means that when working on the top level of your program, args actually refer to the arguments specified on the command line. All arguments are strings, but built-in functions that can convert them to numbers exist.

The previous example functions did not have any return values. This is merely because they didn't need such things. To define a return value, the return value must be sent to the special variable return.

{/a,b/a b ! multiply = return;} = 2mul;
33 3 ! 2mul = 2mult;
stdout 2mult ! write;
The execution of a function does not stop when a value is assigned to return. This has both advantages and disadvantages.

Many things are almost impossible to do without the built-in functions. Some are completely impossible. See Built-In. As all objects in Enigma are pointers, several built-in functions actually modify the objects sent to them as arguments. To solve this problem, one must use the clone function.