Variable scope

BlitzMax Forums/BlitzMax Programming/Variable scope

neilo(Posted 2006) [#1]
Consider the following program:
Strict

Function foo()
	Local bozoTheClown:Int
	
	Function bar()
		bozoTheClown=10
	End Function

End Function 

foo

It doesn't compile... and I'm darned if I can't figure out why. Since bozoTheClown is defined as local within function foo, it really becomes global within the scope of foo. Therefore, my function bar, which is defined within the scope of foo, should see all of foo's variables.

Yet it doesn't.

Now, am I driving the language beyond what it has been designed to do, or is this a bug? There was some talk about Scope...End Scope a while back, but nothing in reference to this.

Any thought?

Regards,

Neil


N(Posted 2006) [#2]
bozoTheClown is still local to foo, regardless of whether or not bar is nested inside of foo. Its scope does not expand beyond foo.


neilo(Posted 2006) [#3]
Darn. Pascal has much nicer scope handeling :(


N(Posted 2006) [#4]
Then why aren't you using it?


WendellM(Posted 2006) [#5]
Since bozoTheClown is defined as local within function foo, it really becomes global within the scope of foo.

It's local within the scope of foo since it's declared as local (which, in BlitzMax, means that it can only be seen at the level declared, neither higher nor lower). If you want it global within foo (so that it can be seen at lower, but not higher, scope levels), then declare it as such, and there's no problem:

Strict

Function foo()
	Global bozoTheClown:Int
	
	Function bar()
		bozoTheClown=10
	End Function

End Function 

foo
Incidentally, this makes it static within foo as well. If that's undesirable, then go ahead and declare it local and just pass it to bar as a parameter (I personally prefer that approach to make coupling obvious).

Yeah, Pascal's scope rules rock, but BlitzMax's are quite serviceable. And think of all the semicolons you're saving! ;)


neilo(Posted 2006) [#6]
WendllM,

Thanks for that!

The reason I wanted to do this was I'm playing with EventHooks within MaxGUI, to try and get LMB/RMB data from gadgets. I couldn't pass parameters via the calling mechanism, so having it global (within the scope of foo) was the way to go.

Personally, I perfer to pass everything as parameters. Saves on unexpected side-effects.


Pascal's scope rules rock, but BlitzMax's are quite serviceable.


They are quite serviceable, and quite logical, too. I went back and read the help file for Global; I can see that the info I wanted was already there. I just didn't fully understand it.


And think of all the semicolons you're saving! ;)



Semicolons don't worry me; it the bloody assignment operator := that I hate!


SoggyP(Posted 2006) [#7]
Hello.

Just querying why you would want a function defined inside a function?

Goodbye.


WendellM(Posted 2006) [#8]
I find myself doing that sometimes. If I'm doing the same thing several times within a function or method, and only there, I'll put the routine in a function within that higher-level function or method. That way, when I look at the code later and try to remind myself what it's for <g>, I know that the inner function has something to do with the outer function/method and nothing else.