t3x.org / t3x / t3x-manual / 283.html
 
T3X - A Minimum Procedural Language
Version 8.1.5, Online Edition
Copyright(C) 1996-2004
Nils M Holm
 
Previous:
2.8.2 Mutually Recursive Procedures
TOC | Index | Back Next:
2.9 The T3X Object Model

2.8.3 Variadic Procedures

In T3X, all procedures have fixed numbers of arguments. It is possible, however, to pass a variable number of arguments to a procedure using a dynamic vector. The following simple example computes the average of n values stored in the vector v:

average(n, v) DO VAR i, t;
        t := 0;
        FOR (i=0, n) t := t+v[i];
        RETURN t/n;
END

Since vectors are first-class objects in T3X, it is possible to inline them in procedure applications, thereby forming an elegant way of passing a variable number of values a procedure:

average(5, [ 2, 3, 5, 7, 11 ]);
average(3, [ (fib(10)), (fac(5)), 789 ]);

Another example is illustrated below. It uses the printf method of the T3X util class and the fib function defined in an earlier section to print the line

fib(n) = m

for each n=1...10 and m=fib(n):

MODULE printfib(util);

OBJECT u[util];

DO VAR i;
        FOR (i=1, 10)
                u.printf("fib(%d) = %d\n", [ (i, fib(i)) ]);
END

U.printf() replaces each %d with the readable representation of the value of one of the arguments in the table. Each time, a %d is processed, the procedure advances to the next argument.

BTW: u.printf() uses the number of %-patterns to determine the number of arguments passed to it.

Previous:
2.8.2 Mutually Recursive Procedures
TOC | Index | Back Next:
2.9 The T3X Object Model