Task

Pass a function     as an argument     to another function.



Related task

   First-class functions


/* Example functions - there are no anonymous functions */
proc nonrec square(word n) word: n*n corp
proc nonrec cube(word n) word: n*n*n corp
 
/* A function that takes another function.
 * Note how a function is defined as:
 *      proc name(arguments) returntype:  [code here]  corp
 * But a function variable is instead defined as:
 *      proc(arguments) returntype name
 */
proc nonrec do_func(word start, stop; proc(word n) word fn) void:
    word n;
    for n from start upto stop do
        write(fn(n):8)
    od;
    writeln()
corp
 
/* We can then just pass the name of a function as an argument */
proc main() void:
    do_func(1, 10, square);
    do_func(1, 10, cube)
corp