Incredible Bmx Feature (Really)

BlitzMax Forums/MaxGUI Module/Incredible Bmx Feature (Really)

beanage(Posted 2010) [#1]
HAH! THIS IS AWESOME!

Strict

Function x()
	Global y:Int = -1
	
	y:+ 1
	Print y
End Function

Rem
Function z()
	Print y 'hah, well, there is no global y .. here!
End Function
End Rem

For Local i:Int = 0 Until 256
	x
Next


Call me Sherlock if this is common knowledge about bmx.. Thanks Mark!

--
Seems likes I was so psyched about this that I posted it in BlitzMax GUI Programming.. Can somebody move the topic to Programming please? :D

Last edited 2010


Jesse(Posted 2010) [#2]
I have been using the concept as static variables in my types and methods for as long as I can remember. I guess I just took it for granted.


xlsior(Posted 2010) [#3]
Inside the function you have a different scope that from within the main program. Likewise, you can also have nested functions inside a function that aren't visible to the rest of the program:

Test()

Function test()
Print "test"
Bleep()

Function bleep()
Print "Bleep!"
End Function

End Function

Bleep()


The second reference to Bleep() (outside of the test() function) will result in an 'identifier not found' error, but the first one from inside the test() function works fine.


_Skully(Posted 2010) [#4]
Oh ya its awesome... it basically creates classes because of the static (global) variables, constants, etc etc. You can burry a whole game in a Type if you wanted to.. although I wouldn't recommend it (extra type instance call for each variable accessed)

Last edited 2010


Czar Flavius(Posted 2010) [#5]
Those globals are useful for debugging purposes. For example, to call a debugstop after the function has been called 100 times.

Another usage I've found - generating unique ID numbers. Have the function increase the global by 1 each time, and return the number. As nothing outside the function can change the global, you can guarnatee you get a unique number each time. Well, supposing you don't use it so many times you run out of numbers ;)


As for nesting functions, I would treat it more as a novelty than a feature to be used, personally.

Last edited 2010