Global inside a Function

BlitzMax Forums/BlitzMax Beginners Area/Global inside a Function

Hotcakes(Posted 2006) [#1]
OK, so a Global when declared inside a Function retains it's previous state when the Function is entered a second time, etc.

But what about something like:
Function MyFunc()
Global file:TImage=LoadImage("file.png")
End Function

Is the LoadImage part only invoked on the first call to that Function (ie Var file is only initialised once) or would I basically be negating the usefulness of a Global inside a Function (Var file is reinitialised each call because of the '=')?


H&K(Posted 2006) [#2]
It keeps its previous state, but you are simply letting it equal somthing each time. ie The firat time its run File doesnt exist. the second time file does exist, it equals whatever but you tell it to equal whatever again

For the behaviour you state you need a doesit exist condition check arround it

Edit. That doesnt sound right does it?


FlameDuck(Posted 2006) [#3]
Is the LoadImage part only invoked on the first call to that Function?
Yes.
Function MyFunc()
	Global test:Int = Confirm("Does this get invoked twice?")
End Function

MyFunc
MyFunc



H&K(Posted 2006) [#4]
Well thats me blown out of the water.

Does that mean you cannot re-allocate a global within a function?

If so is a global in a function a gloryified const?
To use a Global as a real changeable variable Do I need to declare it out side a function?
If I declare it as a field within a type, can I change it, or am I stuck with only the original value?

edit
Test:+1 works though dosent it? Even in Functions, so its only the declareation that is run once. O.K. I get it. Mind It would probably count 1,2,2,3 or something ;)

Thanks


H&K(Posted 2006) [#5]
A Related Question.

When are globals created.

IF I declare a global is it "Created" (Ie actual physical space given to it) at compile time or Run time.

If at run time Is the global created when program flow passes it, or when the program "Initialises"
(Cavat)Is a global created and initialised as a field for a type at the first instance of new for that type, or at the first refference to that type

Thanks


Chris C(Posted 2006) [#6]
Function MyFunc()
	Global test:Int = 1
	Print test
	test:+1
	Print test
End Function

MyFunc
MyFunc


prints

1
2
2
3


so its not as simple as you think


H&K(Posted 2006) [#7]
@Chris, thats exactly what I said it would do in my edit.(Honest).
Youve just read that, and posted yours to make it look like I didnt figure that out myself ;(
(You are a meanie)
Do you have any idea about the other one?


tonyg(Posted 2006) [#8]
so its not as simple as you think


I might be missing something but example *really* is as simple as you think... Isn't it?


Grey Alien(Posted 2006) [#9]
makes perfect sense. The global is intialised once when the program starts, then it can be used like a normal global, but only withing that function.


SoggyP(Posted 2006) [#10]
Hello.

Yep, it makes perfect sense and is as simple as you think.

Function called:
start = 1 : prints 1
add 1 : prints 2

Function called:
stays at 2: prints 2
add 1 : prints 3

What's the issue?

Goodbye.


bradford6(Posted 2006) [#11]

Print "Global in Function"
For x = 1 To 5
	Global_in_Func
Next

Print "Local in Function"
For x = 1 To 5
	Local_in_Func
Next


Function Global_in_Func()
	Global functest:Int 
	Print functest	
		functest:+ 1
End Function

Function Local_in_Func()
	Local functest:Int 
	Print functest	
		functest:+ 1
End Function




u2o(Posted 2006) [#12]
But why would you use a Global? Wouldn't Local be more appropriate? Would Local have the same result?

I can see what the example is doing, but I cant figure out why? When the function is called for the second time, why is the Global not reinitialised and set back to 1?

In my head, it the example should print 1, 2, 1, 2

So many questions...


Byteemoz(Posted 2006) [#13]
The global-in-a-function behaves like any other global variable except that it is being initialised when the function is called for the first time and can only be accessed from within the function. So the example prints:
Global in Function
0
1
2
3
4
Local in Function
0
0
0
0
0



u2o(Posted 2006) [#14]
Ahh, that makes sense :)