Functions

The P* function syntax is equal to that of the C-language. A function needs to have a return value of a specific type. A function may have zero or more arguments. Functions do not inherit variables from the caller. Functions need to have unique names, function overloading is currently not supported. Functions need to be defined inside scenes.

Variables passed into a function are usually copied (pass-by-value). Some objects however, like MySQL-objects, are implicitly passed as reference instead (pass-by-reference). The documentation of these special types tells you which are passed by reference.

Primitive data types like int and string are implicity converted between each other to mach the function arguments (weak type checking).

In the example below, we have a function with return type of int called sum. This function takes two integer arguments and returns their sum. We pass in a string as the second argument to show that it is implicitly converted into an integer when passed into the function.

SCENE main {
	/* Define a function which takes two integer arguments */
	int sum(int a, int b) {
		echo "Adding number $a and $b\n";
		return a + b;
	}

	/* Declare an integer variable 'result' */
	int result;

	/* Call the sum-function and put the result into the 'result'-variable */
	/* The string "3" is implicitly converted into an integer */
	result = sum(1, "3");

	echo "The result is $result\n";
}

This program will output

Adding number 1 and 3
The result is 4