By Reference

BlitzMax Forums/BlitzMax Programming/By Reference

GameScrubs(Posted 2006) [#1]
Can I pass a variable by reference to a function? Docs don't say but it seems to be by value only.


Tom(Posted 2006) [#2]
Use the Var keyword in the parameter section of the Function declaration.
Local a:Int = 10
Doubler a
Print a

Function Doubler(a:Int Var)
  a:* 2
End Function



GameScrubs(Posted 2006) [#3]
Thank you

RAFF


FlameDuck(Posted 2006) [#4]
Also, note that objects are always passed by reference.


GameScrubs(Posted 2006) [#5]
that is weird when I tested it, it treated it by value


dmaz(Posted 2006) [#6]
hmmmm
var does work for "by reference"
Local x:Float = 20

For Local i:Int = 0 To 10
	inc(x)
	Print i+"> "+x
Next

Function inc( i:Float Var )
	i:+1
End Function



Curtastic(Posted 2006) [#7]
you can pass objects by value or by refrence, and it does make a difference

Type tguy
	Field name$
End Type

Local joe:tguy
Global bob:tguy

joe=New tguy
joe.name="joe"

bob=New tguy
bob.name="bob"


change(joe)
Print joe.name

change2(joe)
Print joe.name


Function change(guy:tguy) 'does nothing
	guy=bob
End Function

Function change2(guy:tguy Var)
	guy=bob
End Function