Skip to content

Functions

cairdcoinheringaahing edited this page Sep 24, 2020 · 8 revisions

Functions

Functions are arguably the most important part of Add++, since a large portion of it is based on functional programming. Here is a near complete explanation by the developer in chat logs: How tf do functions work?. This Wiki article attempts to be a full and concise explanation of the features that were touched upon in it.

Functions are generally preferred when trying to execute specific tasks with Add++. They have flexible methods of input and output, and have a lot of handy modifiers to simplify trivial tasks.

There are two main types:

  • Named
  • Anonymous

They work similarly, but their syntax has a couple of crucial differences.

Specialty

The most important thing about functions is the way they handle data. They exclusively use the stack for any operation as opposed to the vanilla code, which uses variables.

All literals push themselves to the stack, i.e. Strings(anything surrounded by " only) and Integers.

All commands are postfix i.e. of the form → <arguments><operator>

Named Functions

Syntax

All named functions have to be made in the following pattern:

D, <name> , <flags> , <code>

Where:

  • Name is the function name, consisting of any ASCII characters except , and >.
  • Flags are specific directives to Add++ which help shorten the code in the function, and make things easier to implement.
  • Code is the function code.

Named functions require you to specify the number of arguments passed to them, which can range from 0 to any number you like.

To define a constant number of arguments, the @ label is used. The number of @s defines the number of arguments.

D,name,@@@, <code>

The above example defines a function name with 3 arguments.

Operators

Most operators in a function operate on the stack and return the value of the result of their operation. As said earlier, they work based on postfix.

Certain special functions like # get applied to the whole stack that the function works on. A list of them will be added soon.

For reference, they are in the interpreter, and match the pattern self.xxx.

Usage

To call a function in a full program, you use the $ and > operators, like so:

$add>arg1>arg2>...

which calls the add function.

Inside of a function, curly brackets are used, like so:

{add}

Inside of a function, there is no requirement to state the arguments, as they are taken directly from the stack, as needed.

For example,

D,f,,{f}

would recursively call the function f forever.

Flags

You have already seen the usage of the @ flag, which is a general requirement for parameterized functions. Add++ has more important flags which help your functions become shorter and more flexible. Here is a list:

Flag Description
! Wraps function arguments in a list.
~ Move all arguments to the stack, flattening all lists
# Reverse the argument order
* Return the entire stack instead of the top element.
^ Stringify the stack and return it joined together
: Output return value

You may use as many flags as you like in your function. Non-flag characters may also be included, but the interpreter will simply ignore them.

Special Flags

These are highly specific flags that the developer doesn't know why they implemented them.

However, they force special functionality in functions which may come in useful for some situation.

All of these take a single argument on the right.

Flag Description
#<var> pushes <var> as an argument.
@<var> Creates <var> with value of the argument passed to it
#@<var> Copies <var> from the main program to the function under the same name.

Anonymous Functions(Lambdas)

A simpler method of defining functions in Add++ is via lambdas. They are much shorter and optimized for general purpose tasks.

Lambdas are variadic, so they can take any number of arguments or no arguments.

Due to this specification, Lambdas use all flags other than @ and ?.

Syntax

Lambdas are written this way:

L<flags>,<code> 

Usage

Lambdas cannot be assigned to variables. Instead, they are numbered from 1 to n, in the order of their creation.

Hence, in normal code, you can call a lambda using it's number like this:

$lambda 1>arg1>arg2>...argn

And in a function:

{$lambda 1}

This calls lambda #1 with the whole stack passed as arguments. The function will take as many elements from the stack as needed.

Furthermore, you can use the ] command while in a function. This takes an integer n from the stack and runs lambda n with the rest of the stack as arguments.

Clone this wiki locally