Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/Using-variables-defined-inside-the-procedure.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

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