Home Accessibility Courses Twitter The Mouth Facebook 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))
Passing back multiple results in Tcl - upvar and uplevel

What's the effect of having a meal? There are many. "You're not hungry any more" is the obvious answer, but also "there's washing up to do", "there's a gap on the shelves" and so on.

And yet when programming with an assignment statement (or a set command in Tcl - I am running a Tcl course this week) there's just a single returned value that is saved to a resut variable.

In Tcl, multiple values could be returned in a list just as in other languages they could be returned in a list / array / hash / tuple / object / dictionary, but Tcl also lets you pass in the names of extra return variables to your proc and then access them within a proc via upvar, or even reach out to variables in the calling code via uplevel.

How does this work?

I wrote an example on yesterday's course - source code [here] - where I was working out how many widgets I could buy for a certain amount of money. The return value was to be the quantity, but I wanted to set a subsiduary result too from which the calling code could ascertain how much money was left. The call:

  set stocklevel [buy $each $cashinhand cashleft]
  puts "You can buy $stocklevel and you will have $cashleft still to spend"


Note that the target variable for the seond return has no $ in front of it. You're passing in the name of the variable, so that you can declare your proc as follows:

  proc buy {unit money_in change} {
      upvar $change payback


which says "The variable who's NAME is passed in in the "change" variable from the calling code is to have another name - an alias - of "payback" in the code of the proc. So that when I set payback:

  set payback [format %.2f [expr $money_in - $unit * $afford]]

I am really setting the variable named in $change in the calling code ... and that's the variable in that calling code called "cashleft".




There's an alternative - which I'm mentioning here for completeness - the uplevel command. With uplevel, you can request that a piece of code within a proc is performed in the variable namespace of the parent. So another way of returning the second value is to set it directly in the "calling scope":

  uplevel "set cashleft $payback"

Under most circumstances, upvar is going to give you better flexibility, though, as the calling code will be able to specify a different variable name each time the proc is called rather than being hardwired, and harder to maintain. There is, however, a new full example of uplevel [here] on the web site.
(written 2011-09-01, updated 2011-09-02)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T207 - Tcl/Tk - Procedures and Variable Scope
  [96] Variable Scope - (2004-10-22)
  [122] Passing arrays to procs in Tcl - (2004-11-18)
  [308] Call by name v call by value - (2005-05-11)
  [409] Functions and commands with dangerous names - (2005-08-11)
  [775] Do not duplicate your code - (2006-06-23)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [1404] Tcl - global, upvar and uplevel. - (2007-10-24)
  [1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
  [2466] Tcl - passing arrays and strings in and back out of procs - (2009-10-22)
  [2476] Tcl - uplevel to run code at calling level - (2009-10-24)
  [2520] Global and Enable - two misused words! - (2009-11-30)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
  [3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
  [3629] Sharing lots of values in Tcl without having lots of global declarations - (2012-02-28)
  [4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)


Back to
If its Sunday, must it be Weymouth?
Previous and next
or
Horse's mouth home
Forward to
User defined sorting and other uses of callbacks in Tcl and Tk
Some other Articles
Tcl packages, pkg_mkIndex, pkgIndex.tcl -what are they and why use them.
What is a namespace and why do we need them?
Storing Tcl source code encoded, and running via your own C program
User defined sorting and other uses of callbacks in Tcl and Tk
Passing back multiple results in Tcl - upvar and uplevel
If its Sunday, must it be Weymouth?
Handling binary data in Perl is easy!
Single and double quotes strings in Perl - what is the difference?
A review of the Summer Sunday extra trains on the TransWilts line
When variables behave differently - Tie in Perl
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/3414_Pas ... level.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb