Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems

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 subjectupdated
4398Accessing 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 ...
3629Sharing 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 ...
3414Passing 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) ...
3192Tcl - 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 ...
1404Tcl - 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 ...
2929Passing 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 ...
2520Global 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 ...
2476Tcl - 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 ...
2466Tcl - 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 ...
1782Calling 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 ...
96Variable 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 ...
1163A 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 ...
775Do 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 ...
409Functions and commands with dangerous names
There are some words that we use in our day to day programming life that seem a little scarey and inappropriate to their real task ... and sometimes those words worry trainees on our courses. Examples: PHP's reset function. It's name seems to threaten to clear our data from somewhere, but all it does ...
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
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.
Proc and return.
Variable scope.
Upvar, uplevel and global.
Structured programming and OO.
Packages and namespaces.
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.

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/resources/T207.html • PAGE BUILT: Mon Feb 8 18:55:24 2016 • BUILD SYSTEM: WomanWithCat