|
proc CMD-NAME ARGS BODY |
|
proc my-cmd {x y} { # Note: { has to be on this line... puts x puts y } |
proc my-cmd {x y} { # Note: { has to be on this line... puts x puts y } |
Sample command invocation:
my-cmd 4 6 |
|
|
|
proc sum {arg1 arg2} { set x [expr {$arg1 + $arg2}]; return $x } |
set x [sum 3 5] set y [sum $x 2] |
The effect can be crippling
proc for {a b c d} { puts "The for command has been replaced by a puts"; puts "The arguments were: $a\n$b\n$c\n$d\n" } |
Result:
|
Example:
for {set i 1} {$i < 10} {incr i} { puts $i } |
|
(We don't need this, so I will skip it)
|
proc my-proc {} { set x 1111 ;# Create a local var x ! puts "Inside my-proc: x = $x" } # ------------------------------- # Main program # ------------------------------- set x 4 ; # Glabel x puts "(1) In main program (outside) before my-proc: x = $x" my-proc puts "(2) In main program (outside) after my-proc: x = $x" |
Output:
(1) In main program (outside) before my-proc: x = 4 Inside my-proc: x = 1111 (2) In main program (outside) before my-proc: x = 4 |
proc my-incr {x} { puts "Inside my-incr before incr: x = $x" set x [expr $x + 1] ;# Change local var x ! puts "Inside my-incr after incr: x = $x" } # ------------------------------- # Main program # ------------------------------- set x 4 # Glabel x puts "(1) In main program (outside) before my-incr: x = $x" my-incr $x puts "(2) In main program (outside) before my-incr: x = $x" |
Output:
(1) In main program (outside) before my-incr: x = 4 Inside my-incr before incr: x = 4 Inside my-incr after incr: x = 5 (2) In main program (outside) before my-incr: x = 4 |
|
Example:
proc my-incr {x} { # You can use another name, e.g.: y upvar x new_x puts "Inside my-incr before incr: new_x = $new_x" set new_x [expr $new_x + 1] ;# Change x ! puts "Inside my-incr after incr: new_x = $new_x" } set x 4 puts "In program (outside) before my-incr: x = $x" my-incr $x puts "In program (outside) before my-incr: x = $x" |
Output:
In program (outside) before my-incr: x = 4 Inside my-incr before incr: new_x = 4 Inside my-incr after incr: new_x = 5 In program (outside) before my-incr: x = 5 |
|
Example:
proc my-incr {} { global x puts "Inside my-incr before incr: x = $x" incr x puts "Inside my-incr after incr: x = $x" } set x 4 puts "In program (outside) before my-incr: x = $x" my-incr puts "In program (outside) before my-incr: x = $x" |
Output:
In program (outside) before my-incr: x = 4 Inside my-incr before incr: x = 4 Inside my-incr after incr: x = 5 In program (outside) before my-incr: x = 4 |
proc my-proc {} { puts "Inside my-proc before set: x = $x" # $x has not been created yet !!! set x 1111 puts "Inside my-proc after set: x = $x" } set x 4 my-proc // Error !! (x undefined in my-proc) |
proc my-proc {} { set x 1111 ;# Creates a local var x puts "Inside my-proc after set: x = $x" } set x 4 my-proc puts $x ---> 4 !!! |