Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Using variables defined inside the procedure

Posted by kinshuk_mishra (kinshuk_mishra), 17 March 2003
Hi,

I have one query regarding TCL variables. I will explain the problem first.....

Problem:

Suppose I define some variable inside a procedure and assign value to it. For ex.

proc test {} {
set x 4
}

Can I access the variable "x" outside the procedure without using "return" command.
If yes then please let me know......

Posted by admin (Graham Ellis), 17 March 2003
You can use either the upvar or the global command.  

Global lets you declare that a variable is the proc is the same as a variable in your main code, and upvar lets you share a variable in your proc with the code one level up (i.e. the code that runs the proc), giving you the opportunity to given the variable a different name in the process - woderful grounds for confusion here!

Here's a piece of code that has the variable called error as a global:

Code:
#!/usr/bin/tcl

# procedure to return a factorial number

# ( e.g. factorial 4 is 4 x 3 x 2 x 1 = 24)

proc factor input {
        global error
        if {$input < 1 || $input >15} {
                incr error
                return 0
                }
        set k [set c 1]
        while {$c <= $input} {
                set k [expr $k * $c]
                incr c
                }
        return $k
        }

###############################################

# Procedure to prompt for and receive a reply

proc askuser {k} {
        global error
        puts -nonewline "$k "
        flush stdout
        set said [gets stdin]
        if {[regexp \[0-9\] $said]} {

                return $said
        } else {
                incr error
                return 0
        }
        }

###############################################

set error 0
puts "Factorial table generator"
set k [askuser "Start at factorial what?"]
if {$error} {puts "ouch (a)"}
set topval [askuser "Up to factorial what?"]
if {$error} {puts "ouch (b)"}

for {} {$k <= $topval} {incr k} {
        set fval [factor $k]
        if {$error} {puts "ouch (c)"}
        puts "factorial $k is $fval"
        }


and here's an example of upvar:

Code:
#!/usr/bin/tcl

# procedure to return a factorial number

# ( e.g. factorial 4 is 4 x 3 x 2 x 1 = 24)

proc factor input {
        upvar error problem
        if {$input < 1 || $input >15} {
                incr problem
                return 0
                }
        set k [set c 1]
        while {$c <= $input} {
                set k [expr $k * $c]
                incr c
                }
        return $k
        }

###############################################

# Procedure to prompt for and receive a reply

proc askuser {k} {
        upvar error error
        puts -nonewline "$k "
        flush stdout
        set said [gets stdin]
        if {[regexp \[0-9\] $said]} {
                return $said
        } else {
                incr error
                return 0
        }
        }

###############################################

set error 0
puts "Factorial table generator"
set k [askuser "Start at factorial what?"]
if {$error} {puts "ouch (a)"}
set topval [askuser "Up to factorial what?"]
if {$error} {puts "ouch (b)"}

for {} {$k <= $topval} {incr k} {
        set fval [factor $k]
        if {$error} {puts "ouch (c)"}
        puts "factorial $k is $fval"
        }




Posted by kinshuk_mishra (kinshuk_mishra), 18 March 2003
Hi Ellis,

Thanks a lot for the reply...really it helped me a lot....although I have come into a different problem now but the point which I had asked is clear....

Thanks a lot once again....

Kinshuk



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho