Functions |
Print Previous Next |
Working With Functions
Creating Functions Functions consist of a head and a body. The head describes the name of the function, its parameters, and its return value. Whereas, the body includes a block of statements, like this one: void function(int p) { if(p * p > 2) do something; do something more; }
The first data type, stated in front of the function, describes the kind of value the function returns and it may be of any known data type. In the case above, no value is returned by the function and therefore void is declared. The actual name of the function can be any name that follows the rules of identifiers in MADRIX Script as was discussed above. But it has to be unique. It is not allowed to have several functions with the same name or with the name of global variables or constants. The parameter list following the name of the function may be left empty, but it is necessary to keep the brackets (). Different parameters are separated by comma. A parameter can take on any data type possible. Here are three examples for function declarations: void setPixel(int point[]) { do something }
int[] CreatePoint(int x, int y) { do something }
string getTag() { do something }
Passing Parameters In MADRIX Script Parameter are always passed via copy by value (The exception are »arrays). This means that a parameter may be used as another local variable of a function. Changing the value of a variable does not change the variable the caller has provided. void testFunc(int i, int ia[]) { i = 5; for(int n = 0; n < i; n++) { ia[n] = n * n; } }
void RenderEffect() { int testArray[]; int len = 2; testFunc(len, testArray); }
In testFunc the parameter i is set to 5 and the array that is passed is filled with several values. After the return of the function in RenderEffect, the array is now filled with the values set in testFunc. Whereas the variable len has not changed and still has a value of 2. Note: Passed parameters are always copied to a function, while this is not the case with arrays.
To return a value, the return statement must be used followed by an expression. The given expression must result in the same or at least a compatible data type of the declared function's type. It must be the last statement of any function which returns a value unequal to void. In addition, return can be used to leave a function early. For void functions return will be used without an expression. Here are some examples: int[] CreatePoint(int x, int y) { int res[] = {x, y}; return(res); }
string getTag() { date d = GetDate(); switch(d.weekday) { case 0: return("Sunday"); break; case 1: return("Monday"); break; case 2: return("Tuesday"); break; case 3: return("Wednesday"); break; case 4: return("Thursday"); break; case 5: return("Friday"); break; case 6: return("Saturday"); break; } return("unknown day"); }
Functions Called By MADRIX
void InitEffect() void RenderEffect() void PreRenderEffect() void PostRenderEffect() void MatrixSizeChanged()
Further Information
|
MADRIX Version: 3.6j | Script Version: 2.22 |
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100% |
Print Previous Next |