|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles Qt Script for Applications

[Prev: Date] [Home] [Next: Number]

Function Type

Functions are normally defined in the application's source code. In some situations it is desirable to create functions at run time.

    var min = new Function( "x", "y", "return x < y ? x : y;" );
    var m = min( 68, 9 ); // m == 9

The first arguments are the names of the parameters; the last argument is a string which contains the function's definition. It is also possible to supply the list of argument names as one comma separated string.

    var min = new Function( "x,y", "return x < y ? x : y;" );

Variable numbers of arguments are also supported, using the arguments array, for example:

    max = new Function(
          "var max = 0;"
        + "for ( var i = 0; i < arguments.length; i++ ) {"
        + "   if ( arguments[ i ] > max ) max = arguments[ i ];"
        + "}"
        + "return max;"
        );
    System.println( max( 50, 16, 24, 99, 1, 97 ) ); // Prints 99

[Prev: Date] [Home] [Next: Number]


Copyright © 2001-2006 TrolltechTrademarks
QSA version 1.1.5