Reflex User Defined Functions - RapturePlatform/Rapture GitHub Wiki

User defined functions

User-defined functions can also be built in Reflex. The main structure of a function definition is show below:

def functionName ( parameters )
    block
end

functionName(parameters);

A simple example of a function being defined and used is in the following listing.

const prefix = "I'll say ";

def sayWhat(name, what) 
   println(prefix + what + " to " + name);
end

sayWhat('Alan', 'hello');
sayWhat('Alan', 42);

Some important points about function declarations and invocations. When defining a function the parameter types are not defined, just their names. So a developer can be very free with the type of parameters as long as the body of the function can also tolerate the type differences. You can see an example of that in the listing above, where the what parameter is also passed as a number as well as a string.

Also variables defined outside the scope of a function are not normally accessible from within the function. You either need to pass the variable as a parameter or declare the variable as const to ensure that it can be accessed within a function. The reason for this is to allow future optimizations of invocations of functions in Reflex - where the function could actually be executed on a different machine than the one used for the outer script. You can see this in action in the script above with the prefix const.

Return to Overview