Numpty returns! - reference question

BlitzMax Forums/BlitzMax Beginners Area/Numpty returns! - reference question

Walla(Posted 2010) [#1]
Hello folks.

I am beavering away with my stupid text parsing and recombining thingumabob and I came across a wierd thing.

if I have a function like this:

Local expanded_fe:Object[] = expand_structure(el_in,0,0)


expand_structure function is recursive and curretly I have some ham fisted bit in there which sets a global variable to the output which I want to return and I just pick it up elsewhere from the global scope. It works but it niggles my puny mind why I can't figure out a better way.

The problem is that when I tried to 'fix' it I actually broke it.

Local expanded_fe:Object[] = expand_structure(el_in,0,0,g_tmp)


I added a variable in the function call, and in the function definition added a parameter to pass through a veriable to hold the output (the same global one I was manually blatting stuff into)

As I wombled around I realised I have no idea why it doesn't work and hoped that one of you fellows who are much more clever and experienced than me can shed some light. I can be surprisingly resistant to understanding sometimes, no idea why!

So what I want to do is pass a parameter to the function which is a reference and at some point in the processing of that function, set the reference to an object array.

Hope I explained it clearly enough, it's probably very simple to you folks but it makes my mind hurt! :D


ima747(Posted 2010) [#2]
Input gets instantiated, meaning when you pass a variable you actually pass the value contained in it.

I don't have any experience with pointer syntax in blitzmax (which is what you want to actually pass the variable itself) so I can't help you there but if you need one value back and it's recursive why not just have it pass the tracked variable back up the chain?

E.g.
Function recursiveFunk:int(int input)
input = input + 1
if(input < 10)
     input = recursiveFunk(input)
End if
Return input

End function


matibee(Posted 2010) [#3]
You can pass a function parameter by reference to the original object using the "Var" keyword e.g:

Function doit ( a:Int Var )
	a = 27
End Function 

Local b:Int = 0
Print b
doit( b )
Print b


but it might be more complicated than that if it's being worked on recursively. Hard to tell from here ;)