Functions

User-defined actions in RapidScript.

Functions are actions the RapidScript program can perform. Functions may or may not require the user to pass in information as parameters before executing, and may or may not return a value after executing. They are identified by a function "signature", consisting of their name and the parameters they take in the following format:

<return type> <function name>(<data type> <parameter name>) {<function body>}

  • Return type: The data type of the value this function will return. If the function does not return a value, the return type will be void.

  • Function name: The name of the function by which it will be referred to in the program.

  • Data type and parameter type: Similarly to a variable declaration, this declares the data type of an input parameter to the function. The value of this parameter can be accessed using the parameter name as a variable name.

  • Function body: A sequence of statements the RapidScript program will execute when this function is called.

For example, here is a basic function that takes no parameters, returns no value, and prints out a message:

void Question()
{
    print("What is the average air-speed velocity of an unladen (European) swallow?");
}

Here is a basic function that takes in a string and integer parameter, then returns a string:

string Answer(int32 velocity, string units)
{
    return velocity + " " + units;
}

Here is an example of using the functions:

void main()
{
    Question();
    string answer = Answer(11, "meters per second");
    print(answer);
}

Which will output the following:

What is the average air-speed velocity of an unladen (European) swallow?

11 meters per second

Last updated