next up previous
Next: Tcl Parsing and Up: Tcl: A Generic Previous: Syntax Summary

Tcl Example

    #!/software/tcl/bin/tclsh

    proc power {base p} {
        set result 1
        while {$p > 0} {
            set result [expr $result * $base]
            set p [expr $p - 1]
        }
        return $result
    }

Now provide a textual user interface to power ...

    set cont "y"
    while {$cont == "y" || $cont == "Y"} {
        puts "Enter base: "
        gets stdin base
    
        puts "Enter power: "
        gets stdin p
    
        puts "$base ^ $p = [power $base $p]"
    
        puts "Continue? (yn): "
        gets stdin cont
    }
    exit


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

cs488@cgl.uwaterloo.ca