Functions in AngelScript

Functions are central in AngelScript. We’ll learn about functions with a few different examples.

void main()
{
    // Function definitions and calls will be demonstrated here
}

Here’s a function that takes two ints and returns their sum as an int.

int plus(int a, int b)
{
    // AngelScript requires explicit returns, i.e. it won't
    // automatically return the value of the last expression.
    return a + b;
}

When you have multiple consecutive parameters of the same type, you need to specify the type for each parameter in AngelScript.

int plusPlus(int a, int b, int c)
{
    return a + b + c;
}

Now let’s use these functions in our main function:

void main()
{
    // Call a function just as you'd expect, with name(args).
    int res = plus(1, 2);
    print("1+2 = " + res);

    res = plusPlus(1, 2, 3);
    print("1+2+3 = " + res);
}

To run this AngelScript code, you would typically embed it in a host application or use an AngelScript interpreter. The output would be:

1+2 = 3
1+2+3 = 6

There are several other features to AngelScript functions. One is default arguments, which allow you to specify default values for function parameters.