next up previous index
Next: Default Arguments Up: Basic Tcl Previous: Loops

Procedures

Procedures have the format

proc name args body
For instance, the Fibonacci function looks as follows:
proc fib n {
    if {$n == 0} {
        return 1
    } elseif {$n == 1} {
        return 1
    } else {
        return [expr [fib [expr $n - 1]] + [fib [expr $n - 2]]]
    }
}

If the procedure has more than one argument, the arguments must be enclosed in braces or quotes to form a list. If there are no arguments, an empty string is specified:

proc succeed {} {
    return 1
}
Note also that there must be a space between the argument list and the body opening brace.





Micha Meier
Tue Jul 2 09:49:39 MET DST 1996