upvar: point in tcl

When we need pass a variable name to a procedure, we can use upvar. upvar like reference in C++, we can changes the variable as we like.

the syntax of upvar is:

upvar ?level? varName localvar 

When we need pass a variable name to a procedure, we can use upvar. upvar like reference in C++, we can changes the variable as we like.

the syntax of upvar is:

upvar ?level? varName localvar 

example:

#!/usr/bin/tcl

proc swap { x y} {

    upvar x a

    upvar y b

    set tmp  $a

    set a    $b

    set b    $tmp

}



set x 10

set y 5

swap x y

puts "x is $x"

puts "y is $y"  

result:

x is 5

y is 10

Leave a Reply

Your email address will not be published. Required fields are marked *