Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Variables 1

Status
Not open for further replies.

JRyanCon

Technical User
Aug 19, 2004
47
0
0
US
If you set a variable and want to use that variable in a Proc you have to use the "global" command on that variable. Then it becomes usable. Like:

=======
set foo "10"

proc bar {} {
global foo
puts "$foo"
}

=======

This would print 10

I understand this. However, what if I want to use a variable outside a proc that I started and modified in that proc? for instance:


========
set foo "10"

proc bar {} {
set var "5"
}

bar
puts $var
===========

how would I go about this? What i actually have is a procedure thats will run X times and when it runs I want to incr a value then call that value outside the proc.

Anyone care to clear this up for me?

 
Once again use global.
Code:
proc makeglobalresult {n} {
global res
          set res [expr int(1 + rand() * $n)]
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top