Variable Global No Work.

BlitzMax Forums/BlitzMax Programming/Variable Global No Work.

Yue(Posted March) [#1]

Function nameFunction()

        Global nameVariable = 1


 

End Function 


nameFunction()


Print nameVariable





I'm a little confused return print number 0.


skidracer(Posted March) [#2]
To avoid confusion it is good to use Strict mode in BlitzMax.

When you declare a Global inside a function it is only visible from inside the function (but unlike Local the value stored persists between calls).

Without strict BlitzMax creates another nameVariable that is unrelated to your Global.

With strict you should get a helpful error.


gpete(Posted March) [#3]
Function nameFunction()
Global n:Int=10
Return n
End Function


Print "howdy there Pardner!! "
Print nameFunction()



End


Floyd(Posted March) [#4]
The example implies he wants a Global variable, visible outside the function.
Global nameVariable

Function nameFunction()

	nameVariable = 1

End Function 

nameFunction()

Print nameVariable



Derron(Posted March) [#5]

When you declare a Global inside a function it is only visible from inside the function (but unlike Local the value stored persists between calls).



I would have thought it is available from everywhere. Hence "globally" available. A bit misleading IMHO. But you are right...strict catches that issue. I never had that issue as I create my globals outside of functions or as "property" of a type (which already exposes that visibility limitation I just blamed. Hmm.).

Bye
Ron


Henri(Posted March) [#6]
Hi,

globals can be stored inside type namespace also for more sanitary code.

Strict

Function nameFunction()
	MyName.variable = 3
End Function 

nameFunction()

Print MyName.variable

Type MyName
	Global variable:Int
EndType


Globals are created in the beginning so all globals exist at code execution time. We just don't have a handle to access global declared inside a function outside that function. Functions themselfs are globals too.

-Henri


TomToad(Posted March) [#7]
Global behaves differently depending on where it is used. Outside of any functions or types and it behaves as you would expect, a variable which can be used anywhere in the program. When it is defined in a function, it behaves similar to a local except that it will retain its value between calls to the function. When it is defined in a type, one copy of the variable will be used on all instances, so when it changes in one, it will change for the others as well.



degac(Posted March) [#8]
..and to add a little more confusion :)


SuperStrict

Global total1:Int=100
Global total2:Int=1000

Print "'real' Global Total1 "+total1
Function1()
Print "'real' Global Total1 "+total1
Function1()


Function Function1:Int()
 Global total1:Int=200
 'in the function 'total1' is a local just to Function1 (and InsideFunction) BUT it keeps the values (a simple Local var will be reset)
 'it's not related with the 'previous and extern' total1 
 'they have the same name, but internally they are something different (like main.total1 and main.Function1.Total1

 Print "Function 1: Total1 : "+total1
 Print "Function 1: Total2 : "+total2 'this is a real GLOBAL

 InsideFunction()
	
	total1:+10

 Function InsideFunction:Int()
  'yes, complication ahead!!! 
  '
  Print "Function 1: Total1 : insideFunction : "+total1
  Print "Function 1: Total2 : insideFunction : "+total2
  total1:+1
 End Function

End Function




grable(Posted March) [#9]
degac's post isnt really about Globals, but about Variable Shadowing. Which you can do with any kind of variable anywhere, so long as your in Strict mode or better.


col(Posted March) [#10]
..and to add a little more confusion :)

Which can be added to even more by using the '.' operator to access the outer most global variables and functions :)

Print "Function 1: The global 'Total1' in Function1 Total1 : "+total1
Print "Function 1: The 'real global Total1' accessible to the rest of the code : "+ .total1 'this is the outermost GLOBAL Total1


But yes as grable says... this is referred to as shadowing.


FireballStarfish(Posted March) [#11]
..and to add a little more confusion :)


Don't forget that they're visible only below the line where they're declared - except when you're referencing them from within a more deeply nested function or from a type:
SuperStrict
Framework BRL.StandardIO

Global g:Int = 1

F

Function F()
	Function F2()
		Print g
	End Function
	F2
	
	Print g
	
	Global g:Int = 10
	
	Print g
End Function



degac(Posted March) [#12]
Well, BlitzMax is so lovely in its syntax, don't you? :)

ps: I really missed the '.'(global) thing (or maybe I decided to remove it from my memory to avoid too much confusion!)