Modifying a global in a function

BlitzMax Forums/BlitzMax Programming/Modifying a global in a function

Damien Sturdy(Posted 2007) [#1]
Hi All,

Couldn't think of an easy way to word the question!

What I need to do is as follows:

Is it possible in max without having to give the function a pointer?

global A:int=100

test(a)

Print A

Function test(inp:Int)
	inp=10
End Function



EOF(Posted 2007) [#2]
Global A:Int=100

test(a)

Print A

Function test(inp:Int Var)
	inp=10
End Function



Damien Sturdy(Posted 2007) [#3]
Bingo! Cheers!


ImaginaryHuman(Posted 2007) [#4]
Since A is a global, it is already available to the function as A, you don't even need to pass it as a parameter.


Damien Sturdy(Posted 2007) [#5]
AngelDaniel, thanks, but Jim brown got what I was doing.

If I passed any variable at all to the function as A, and I modify A, then the variable I passed to the function is what gets changed. He hit it on the head and got it working.

Thanks :)


Grey Alien(Posted 2007) [#6]
yeah but as AngelDaniel says, if A is already global, you don't even need to pass it into the function, it can just be modified in the function as A (unless you want to rename/"alias" it for some reason)...


ImaginaryHuman(Posted 2007) [#7]
Thanks GreyAlien. I guess what he's trying to do is to have any kind of variable be passed and be modified in its original form, regardless of whether it is global or local or otherwise, and perhaps just defined it as a Global in his example out of habit or whatever, and maybe has been accustomed to having to use globals to achieve this result due to not knowing about Var. Or.... maybe we're right ;-D


Grey Alien(Posted 2007) [#8]
who knows what the Cygnus thinks...?


Damien Sturdy(Posted 2007) [#9]
Lol, you guys make me laugh!! :)

Of course, I know how a global works.


guess what he's trying to do is to have any kind of variable be passed and be modified in its original form, regardless of whether it is global or local or otherwise, and perhaps just defined it as a Global in his example out of habit or whatever, and maybe has been accustomed to having to use globals to achieve this result due to not knowing about Var



Exactly :)

to quote myself;


Couldn't think of an easy way to word the question!



I hadn't heard of Var before today, though I knew what I wanted to do was achievable. I didn't want to have to pass a pointer to the function! :)


ImaginaryHuman(Posted 2007) [#10]
I have found that var is the same speed as passing a variable. You'd think it would be more efficient since a new variable is being created normally, but I didn't find it to be any faster, otherwise I would use it extensively.


H&K(Posted 2007) [#11]
Ok. Dumb noob question. This only work for Internal types?

That is, it was my unerstanding that an internal type was passed by value, but a defined Type was passed by pointer already (ie already by Var ish).

So in Cygnus' first example if the global Had been a defined type, his example would have worked

Is this totaly wrong?


grable(Posted 2007) [#12]
This only work for Internal types?

Var works for any type of variable.

but a defined Type was passed by pointer already (ie already by Var ish).

Your partly right, except that the address of the variable itself is passed to a Var parameter, instead of its contents (ie the type reference).


Dreamora(Posted 2007) [#13]
BM never sends the contents.
What you normaly send is a reference which is a typesafe pointer to the contents. (unlike old blitz)