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
 
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))
upvar - do I need to use it?

Posted by admin (Graham Ellis), 16 August 2002
An example of upvar

The upvar command is vital to most Tcl authors as it allows you to pass
in the name of a variable to a proc, then modify the variable contents
within that proc.  It isn't too obvious how it works at first, though, and
it's very different to other programming languages.   Here's an
example for you to look through if you're unclear as to how it works.


Code:
proc double {tommy} {
       upvar $tommy numbah
       set numbah [expr $numbah * 2]
       return 1
       }


set fred 12
set ethel 16
double fred
double ethel
set howidid [double ethel]
puts $fred
puts $ethel
puts $howidid


Scenario - I want to write a proc that doubles the value of an incoming
variable, and puts the result back in the same variable; the value actually
returned by the proc is to be "1" for success (and perhaps 0 for failure in
an extended version?).

When I call my proc, I pass it the name of the variable (so there's no $
character needed), and the variable tommy ends up containing, literally, the
name of the incoming variable - in our example, that's ethel or fred.

The upvar command turns the variable numbah in our proc into the same variable
(and NOT just a copy) as ethel, then as fred, then as ethel again at each
successive call in our example, and thus any changes made to numbah are also
changes made to ethel and fred.

The value returned by double is something quite different to fred, ethel,
tommy or numbah;  in one of our calls, we saved a copy of the return value
into howidid to demonstrate the existance of the quite separate return value.





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., 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