User Defined Functions
User-defined functions allow you to create your own MP console functions. You can save your own functions in text files for later use. By simply loading the text file, the functions will seamlessly integrate in the application and will work just like other inbuilt functions.
Please remember that User Defined Functions are interpreted at run-time and due to that are far less efficient than the in-built functions. If you want full performance, you have to create new functions in a .Net language (like C#) and add them to MP using its add-on system.
User functions can be called by passing named or anonymous arguments just like any regular functions. You can return values from the user functions and can also make recursive calls.
Creating User Defined Functions
The special construct named "function" is used to create user defined functions. It has the following syntax:
name = function ( [return], paramDef_1, ... , paramDef_m ) { command_1: .... , command_n : }
name: It is the name by which the newly created function will be known. If the function is successfully created, you will use this name to call the function.
return: If the functions returns a value, it notifies this fact by providing the word return as the first argument to function construct. The special variable return can then be assigned a value in the function body which is returned to the caller.
paramDef: paramDef_1, paramDef_2 etc. are the parameter definers of the function. It is up to you to decide what parameters to use. paramDef has a very special format. It is a string literal having one of the following four forms:
(i) v_... (ii) s_... (iii) b_... (iv) m_
The dots (...) after each of the above strings actually mean any characters of your choice. For example v_00, s_abc, b_MyBool etc.
v_, s_, b_ and m_ are, in fact, special prefixes, which inform the function construct about the type of the parameter. This also implies that parameters can be one of the four possible types. These are:
v_: Parameter is a vector (any number or array)
s_: Parameter is a string (plain text).
b_: Parameter is a Boolean value or array
m_: Parameter is a Matrix
Inside the function you will use these identifiers to refer to the actual arguments passed. Math Processor will use the names for run-time identification of the arguments.
Example-1
The following statement will create a user defined function that will create Spirograph patterns:
spirograph = function (v_R, v_r, v_p, v_nRotations, s_color)
{
t = vectorin(0, 0.05, 2 * pi * v_nRotations);:
x = (v_R + v_r) * cos(t) + v_p * cos((v_R + v_r) * t / v_r);:
y = (v_R + v_r)* sin(t) + v_p * sin((v_R + v_r) * t / v_r);:
plot(x, y, s_color):
}
To see this function in action, execute the following statement:
spirograph(53, -29, 40, 30, gray)
Example-2
The following MP code spinnet creates a user defined function named fibonacci, which, not surprisingly, tells the Fibonacci number at the specified offset:
fibonacci = function (return, v_times, v_twoNumbersBack, v_oneNumberBack)
{
if (v_times == 1) { return = v_twoNumbersBack;:}
else
{
if (v_times == 2) { return = v_oneNumberBack;:}
else
{
if (v_times == 3) { return = v_oneNumberBack + v_twoNumbersBack;: }
else
{
if (v_times >= 0)
{
return = fibonacci(v_times - 1, v_oneNumberBack, v_oneNumberBack + v_twoNumbersBack);:
}
else
{
echo("Error: Bad input."):
return = -1;:
}
}
}
}
}
Now we can call this newly created function just like any other inbuild function like this:
fibonacci (10, 0, 1)
The above invocation returns the Fibonacci number at index 10.
Incidently MP provides a built-in function called fib, which does exactly the same thing. So this User Defined Function is actually redundent. But, it served the purpose quite well, did it not?