Cover by Don Libes

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Passing By Reference

By default, you can only refer to global variables (after using the global command) or variables declared within a procedure. The upvar command provides a way to refer to variables in any outer scope. A common use for this is to implement pass by reference. When a variable is passed by reference, the calling procedure can see any changes the called procedure makes to the variable.

The most common use of upvar is to get access to a variable in the scope of the calling procedure. If a procedure is called with the variable v as an argument, the procedure associates the caller’s variable v with a second variable so that when the second variable is changed, the caller’s v is changed also.

For example, the following command associates the variable name stored in name with the variable p.

upvar $name p

After this command, any references to p also refer to the variable named within name. If name contains "v“, "set p 1" sets p to 1 inside the procedure and v to 1 in the caller of the procedure.

The qf procedure can be rewritten to use upvar. As originally written, qf returned a list. This is a little inconvenient because the list always has to be torn apart to get at the two values. Lists are handy when they are long or of unknown size, but they are a nuisance just for handling two values. However, Tcl only allows procedures to return a single value, and a list is the only way to make two values “feel” like one.

Here is another procedure to compute the quadratic formula but ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required