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.


Prior to v-, paramDef used to have a prefix that identified it's type. Since v-, user defined functions do not use prefixes. No runtime checks are performed on function arguments and the caller is reponsible for passing correct number and type of parameters. This has greatly optimized the UFD's and made them more readable too.


The following statement will create a user defined function that will create Spirograph patterns:

spirograph = function (R, r, p, rotations, color)
    t = vectorin(0, 0.05, 2 * pi * rotations);:
    x = (R + r) * cos(t) + p * cos((R + r) * t / r);:
    y = (R + r)* sin(t) + p * sin((R + r) * t / r);:
    plot(x, y, color):

To see this function in action, execute the following statement:

spirograph(53, -29, 40, 30, gray)


The following MP code snippet creates a user defined function named fibonacci, which, not surprisingly, tells the Fibonacci number at the specified offset:

fibonacci = function (return, times, twoNumbersBack, oneNumberBack) 
    if (times == 1) { return = twoNumbersBack;:}
    elseif (times == 2) { return = oneNumberBack;:}
    elseif (times == 3) { return = oneNumberBack + twoNumbersBack;: }
    elseif (times >= 0)
        return = fibonacci(times - 1, oneNumberBack, oneNumberBack + twoNumbersBack);:
        echo("Error: Bad input."):
        return = -1;:

The above code is using the 'elseif' construct, which was introduced
in v-

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.

incidentally, MP provides a built-in function called fib, which provides the same functionality (only in a better way). So this User Defined Function is actually redundant. But, it served the purpose quite well, did it not?


New Version!

Spirograph (desktop) v- was released on April 23, 2015. Improvements include:

  • Fill colors (with transparency)
  • Two fill modes
  • High resolution export for designers