For 2023 - 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)) |

Well House Consultants
You are on the site of
Well House Consultants
who provide
Open Source Training Courses
and business
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
|
Tcl/Tk module T207
Procedures and Variable Scope
Exercises, examples and other material relating to training module T207. This topic is presented on public courses Learning to program in Tcl, Tcl Programming, Tcl Programming
As your code grows from 1 to 10 to 100 lines, you'll find that it's helpful to split it into a series of logical blocks, each of which can be run as a command. You'll learn how to define such procedures, how to pass information in and out of them, and how to access information that's held in your main program and in the calling procedure too.
Articles and tips on this subject | updated | 4398 | Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl In a perfect programming world, code is broken into named blocks (methods, functions, subroutines, classes, procedures, commands, modules, packages etc) and data is passed around between the blocks purely as parameters and return values. And variables are local to the block in which they're used. This ... | 2015-01-18 | 3629 | Sharing lots of values in Tcl without having lots of global declarations Q: How can I make a lot of values defined in my Tcl main program available in various procs, without having to pass them all down the chain or declare them as global all over the place?
A question from today's Tcl course - to which my first reaction was "why do you want to do that?". However, having ... | 2012-03-03 | 3414 | 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) ... | 2011-09-02 | 3192 | Tcl - Some example of HOW TO in handling data files and formats During the Tcl course I was running earlier this week, we got involved in a number of interesting topics such as
• How to clean up input lines into lists of fields (use split)
• How to reformat awkward fields (use regexp and regsub)
• How to combine files that vary in format from year ... | 2011-03-04 | 1404 | Tcl - global, upvar and uplevel. In Tcl, variables are "scoped" to the procedure in which they're used. That's a sensible default, since you want to be able to write main applications without the risk of interfering with variables within your procs.
A global command in a proc allows you to refer to a variable in the main code from ... | 2011-03-01 | 2929 | Passing a variable number of parameters in to a function / method How many columns are there in each row in a table? It will vary, depending on the table!
If I want to write a "row" function in PHP, passing in as parameters each of my columns, I don't know the parameter count. But I can find out using the fun_num_args function, and I can then get each of them using ... | 2010-08-20 | 2520 | Global and Enable - two misused words! The word global is used in declaring variables in some languages such as Tcl and Python to indicate that the variable being referred to is shared with the variable of the same name at the top scope. To use the word global, which implies that the declaration makes the variable visible everywhere, is misleading
The ... | 2009-12-09 | 2476 | Tcl - uplevel to run code at calling level Tcl procs (they're Tcl's functions) can pass information back via the return command, and they can access data in the top level of code via the global commands. Incoming parameters can be passed in 'by name' so that any values altered within the proc are also altered in the calling code - there's an ... | 2009-10-24 | 2466 | Tcl - passing arrays and strings in and back out of procs When you want to pass data INTO a proc in Tcl from a regular variable, you write the variable name with a $ in front of it in the code and the value is substituted. But this doesn't work if (a) you are passing an array (which cannot be expanded into a string or (b) you want to change the value in a ... | 2009-10-22 | 1782 | Calling procs in Tcl and how it compares to Perl In languages such as Java, you must call your named blocks of code (methods) with the correct number and type of parameters, but in Perl you may call them with as many or as few parameters as you like ... then write the named block of code (a sub) to handle (or ignore) whatever it gets.
This week, I'm ... | 2008-09-04 | 96 | Variable Scope One of the vital topics on all our programming courses is that of variable scope. Variable Scope may be defined as the area of a program in which a variable is visible, and how long that variable is accessible for.
Why do I describe variable scope as a vital subject when you can write simple programs ... | 2008-05-11 | 1163 | A better alternative to cutting and pasting code If you're new to coding, you'll be so concerned to be writing code that works that you may not take a look at coding technique. Your nose will be so close to the grindstone as you work that you won't take the time to look and ask "Do I need to keep grinding anyway?"
If you find yourself writing a piece ... | 2007-04-26 | 775 | Do not duplicate your code If you've writing or maintaining a program and you find yourself cutting and pasting a chunk of code, STOP and think again.
By duplicating a block of code, you're duplicating your maintainance task from that point onwards - any fixes applied to the original much be applied to the copy too. And that's ... | 2006-06-30 | 122 | Passing arrays to procs in Tcl If you're writing a Tcl proc, you'll normally pass in parameters by value, specifying a $ in front of the variable whos contents you want to pass in in the calling sequence:
dojob $passin
If, however, passin is a Tcl array, tis won't work since $passin cannot be expanded to string. So you'll ... | 2006-06-05 |
Examples from our training material
add2 | Answer to final exercise | addindemo | Sample code to complete - final exercise | another | upvar to return a second value | cbn | Using upvar to call by name | doublit | Another upvar example for learners | dr2 | Optional parameter in Tcl | dresser | Changing a value within a proc and returning the altered value | gsc | The global scope | knots | Unloading an array of inputs as local variables in a proc | p1 | Sharing a variable using global | p2 | Sharing a variable using upvar | p3 | Sharing a variable using upvar and call by name - RECOMMENED | p4 | returning a variable - RECOMMENED for passing 1 piece of data back | p5 | Sharing a variable using uplevel - obscure! | pr1 | Defining a proc (command) and using it | pr3 | loading a file of procs shared with other applications | scope1 | Variable default to being local | tied | Using an array of inputs as if they were local variables in a proc | up1 | Using upvar to effect a variable in the calling code | uquery | Shared file of procs | uv | Call by value and call by name | vardemo | Calling a proc with a variable number of arguments | whatandwhen | Exercise - please write the proc to complete | wwanswer | First sample answer - "getnumber" | wwbetter | Better sample answer - "getnumber" | yetan | uplevel to store a value in parent |
Pictures Radia software is tailored through Tcl procedures
Teaching Tcl around the world
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from [here].
Topics covered in this module
Holding commonly used command sequences. Why use procs?. Sharing procedures with another application. Variable parameters. Variable scope. The global scope. The upvar command. Another example of upvar. Structured programming and OO. Structured programming. Programming with objects. Use of Procs as an interface to Tcl.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.
Well House Consultants specialise in training courses in
Tcl/Tk, Expect, Ruby,
Lua,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.
|
|
|