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!

Variables in functions 1

Status
Not open for further replies.

jliz2803

IS-IT--Management
Mar 21, 2005
45
CA
Is it possible to declare and assign a variable in one function and then use that same variable to test a condition in another function?
 
If you use a variable for the first time in a function without declaring it with var, then it is a global variable, and can be manipulated in other functions. Another option is to declare it outside all functions and either assign no value to it, or assign null to it.

Lee
 
Lee is right.
Any variables you declare before functions are considered global.
var doofy="";

Any reference to the variable doofy in or out functions refers to the SAME doofy.
I don't know, however, if, when you declare a var doofy in a function after that, it makes a local doofy (though I'm pretty sure it is in C and its variations).
Was that English? lol

 
If you use var to declare a variable in a function, then it's a local variable. If you don't use var, then the variable is global.

Any variable declared locally in a function is a local variable, and you won't be able to access the global variable when there's a local one with the same name. Learned this by experience when I forgot to use var one time in a function, and what was supposed to be a local variable affected a global one. Using var fixed the problem with no other changes.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top