AFL

Arguably Functional Language



AFL Documentation

Functions


Functions are ways to easily group a bunch of code together to run at once, on command. This is incredibly powerful because it makes code shorter and easier to read. Function syntax involves two parts: creating functions and calling (running) them.

Calling functions

if you're reading this, you've already called a function with AFL. when you wrote 'help(7)', you called the function help. yep, the help system is a function, and it's actually written in AFL code. The syntax for calling a function is as follows:

functionName( parameters )

every function has a certain number of parameters it expects, and needs to be given that amount. parameters are separated by spaces (if required). Here are some examples: the function 'clear' expects no parameters, and can be called as 'clear()' the function 'pow' expects two parameters and could be called like 'pow(3 3)' It should be noted that function calls can both contain expressions in their parameters and be part of expressions.

Creating functions

Functions work similarly to control statements, in that the function definition creates a block of code ended by the end() function. The definition of a function is itself a function (kinda), and looks like this:

function(functionName params);

functionName is of string type, and will the name of the function when you call it. It can be any combination of letters, and it cannot include any numbers, spaces, or symbols. params is of num type, and is the amount of parameters your function expects. Note that expressions cannot be used in the parameters for the function function. Only constants. here is an example of a function.

Also note that function definitions ignore other code blocks, and whether code in a specific block is run does not effect whether the function is defined. It is customary to define functions outside of any other code blocks to make code more understandable.

function("example" 0); "hello there!"; # isn't this a cool function? no? whatever; "more words!"; end()

this function can then be called with 'example()'.

Each function comes with an array called 'params', which holds all of the parameters that the function has been called with. It is accessed with array related functions as normal. See the entry on arrays for information about handling them.

Functions can also return values. this is accomplished through the return function. the return function can have zero or one parameters. If it has 0, it ends the function's execution and returns nothing. if it has 1, it ends the functions's execution and produces that value.

Here is an example function that increments by 1 the number that is put into it.

function("increment"1); # get the first parameter; toReturn = arrayGet(params 0); # increment and return it; toReturn = toReturn + 1; return(toReturn); end(); # some test cases; increment(1); # prints 2; 4*increment(4); # prints 20; increment("zebra fish"); # prints 'zebra fish1'; increment(true); # prints 'true1'

The last examples produce some interesting behavior, because the increment function didn't handle receiving parameters of unexpected types. For some cases this might be fine, but if you want to create a more robust system, AFL provides a function that simplifies the ordeal, called checkParam. Check out the reference for the util component for more information.