Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Functions

XMLang supports functions, which are reusable blocks of code.

Functions can take attributes and children, which are used as parameters. They can return a value, which can be used in the calling code. They can be called from anywhere in the program, including inside other functions, provided they are defined before the call.

Unlike anywhere else in the program, functions have their own local scope. They can't access or modify variables in the global scope, and values of the local variables defined in the function do not persist between function calls.

<function>

The <function> element is used to define a function.

Attributes

  • name (string): The name of the function.

Children

This is a block. Its children are saved and will be executed when the function is called.

<call>

The <call> element is used to call a function.

Attributes

  • name (string): The name of the function to call. If the function does not exist, an error will be thrown (Function `{name}` not found).
  • Any other attributes: see specials below.

Children

It accepts any number of children, which are evaluated and passed as parameters to the function. See specials below.

Specials

The body of the function (children of <function>) can access the attributes and children passed to the function using the <special> element with the name attribute set to:

  • The name of the attribute passed to the <call> element to retrieve the value of that attribute as a string.
  • child_count to retrieve the number of children passed to the <call> element as an int.
  • child:{index} to retrieve the value of the child at the specified index (int). The index is zero-based, so the first child is child:0, the second child is child:1, and so on.

Example

<program>
    <function name="greet">
        <set var="person">
            <if>
                <condition>
                    <eq>
                        <special name="child_count" />
                        <int>0</int>
                    </eq>
                </condition>
                <then>
                    <special name="person" />
                </then>
                <else>
                    <special name="child:0" />
                </else>
            </if>
        </set>
        <print>Hello, <space /> <get var="person" />!</print>
    </function>

    <call name="greet" person="Alice" />
    <call name="greet">Bob</call>
</program>