Functions |
|
Functions allways have a return type of var. If a function does not explicitly return a value, an empty string will be returned. Since each function returns a var, it is not necessary to put var in front of the function header, although you may do so to indicate that your function ought to return someting meaningful: var foo() // the 'var' is optional { return 'hello'; } // foo main() { const x = foo(); // x = 'hello' } // main The parameter list may have a fixed or variable number of parameters. Optional parameters are enclosed in braces [ ]: fixParam(var a, var b) // 2 mandatory parameters ... varParam(var a, [var b, var c]) // 1 mandatory and 2 optional parameters ... If there are optional parameters, CSS generates a local const namedargCount holding the passed number of arguments. CSS will validate that the number of arguments passed isn't less than the number of mandatory parameters and also not bigger than the total number of parameters. Your function must validate the number of optional parameters before accessing them: foo(const a, [const b]) { if (argCount == 2) sysLog('you passed 2 args: '+a+' and '+b); else sysLog('you passed 1 arg: '+a); } If your function doesn't change the arg values you may define themconst instead of var so they are protected by CSS. CSS also supports parameter passing by reference with the operator & (as known by C++). In fact arrays may only be passed by reference and not by value: #loadLibrary 'KcSyslib' groom(var& a, var &b[][]) { const maxDim = 10; a = 'groom'; const dim2 = sizeof(b[0]); const dim1 = sizeof(b) / dim2; if (dim1 > maxDim || dim2 > maxDim) throw '%%% groom was designed for dims <= '+maxDim; for (var y = 0; y < dim1; y++) for (var x = 0; x < dim2; x++) b[y][x] = y*dim2+x; return a+' done'; } main() { var aa; var bb[3][4][5]; sysLog(groom(aa, bb[1])); // groom done sysLog(aa); // groom sysLog(bb[1][2][3]); // 13 } Array-parameters never have an explicit index as you see in the example above. If your function imposes restrictions on array size(s), it's up to your function to verify that by use of the sizeof operator as we did in the example above. | ||||||||
Copyright © IBK Landquart | Last revised by Peter Koch, 24.02.00 | << Back Top Next >> |