next up previous
Next: Lists Up: Tcl: A Generic Previous: Control Structures

Procedures

Syntax:
proc name listOfArguments Body
    proc add {x y} {
        expr $x+$y
    }
Semantics:

Arguments can have defaults:
    proc decr {x {y 1}} {
        expr $x-$y
    }
Variable-length argument lists are declared using args:
    proc sum args {
        set s 0
        foreach i $args {
            set s [expr $s+$i]
        }
        return $s
    }
Examples:
sum 1 2 3 4 5

15
sum

0



CS488/688: Introduction to Interactive Computer Graphics
University of Waterloo
Computer Graphics Lab

cs488@cgl.uwaterloo.ca