Passing Parameters by Reference?

BlitzPlus Forums/BlitzPlus Programming/Passing Parameters by Reference?

rogue(Posted 2005) [#1]
I need to have a routine modify my input parameters, but Blitz doesn't seem to support 'ByRef'. I am sure other people have had to do something similar (particularly for collision and math functions).

Here is an example:

Function Normalize2D( x1#, y1# )

Local vLen# = Sqr( ( x1# * x1# ) + ( y1# * y1# ) )

If vLen# < 0.00001 Then
x1# = 0.0
y1# = 0.0
Else
x1# = x1# / vLen#
y1# = y1# / vLen#
EndIf

End Function

The modified values for x1, y1 need to be returned to the caller of the function.

The only thing that appears to come close to this is using a bank which seems like a lot of code overhead for such a simple thing.

Thanks,

- Ken


Grey Alien(Posted 2005) [#2]
This lack of functionality disappointed me too.

When you say "bank" do you mean an array that you defined earlier with Dim?

Obviously you could use global variables for x# and y# but this may not be desireable. OR you could make a type called Point with 2 fields of x# and y#. You can pass the Point in by calling the function as

Normalize2D(p.Point)

then you can modify the x# and y# as p\x and p\y respectively. This works find for me although I tend to use bigger types such as ManType with 30+ fields. There could be a processing overhead if you are to call the function loads. You could even lamely include the code in the calling function instead of calling your Normalise2D (english ;-)), this is the fastest of all but not very "future proof" or reuseable.


CS_TBL(Posted 2005) [#3]
If it's about returning more than 1 var from a function (one of the advantages of reference vars): return a string! :)

;
; Return more than 1 var from a function
;
; stupid idea by: CS_TBL  ^____^
;

f1#=.9
f2#=.8

r$=bla$(f1#,f2#)

f1#=Mid$(r$,1,8)
f2#=Mid$(r$,9,8)

Notify "<"+f1#+"> <"+f2#+">"

End

Function bla$(a#,b#)
	a#=a#^3
	b#=b#^3

	Notify a#
	Notify b#
	
	Return RSet$(Left$(a#,8),8)+RSet$(Left$(b#,8),8)
End Function


As you see, this method allows you to return even more variables than you could return using reference vars, I guess one can return hundreds of vars this way :)

I dunno about banks.. they're a lil more comfy I guess, but I dunno about the potential mem they eat, if you don't free them (=overhead) .. strings are kinda harmless I think..

Well, cya at the weekly group-session-talk at "the assylum for B+ users who didn't buy Bmax yet" o_O


Kevin_(Posted 2005) [#4]
Returning a string is the best option in my opinion because you don't have to define anything before hand like an array or bank.


Grey Alien(Posted 2005) [#5]
What is a bank? please ...


CS_TBL(Posted 2005) [#6]
see the help for CreateBank()

It's a piece o' mem

bla=CreateBank(10) ; creates a piece o' mem of 10 bytes

With PeekByte,PokeByte ..Int, ..Float, ..Short you can read/write these values to banks. If you put variables/values in banks (on fixed locations) then it's all very low-level stuff :)

A bank can be created and used locally in a function, that's what makes them handy.

bank=MakeBank()
Notify bank
Notify BankSize(bank)
FreeBank bank
End

Function Makebank()
  Return CreateBank(10)
end Function



Grey Alien(Posted 2005) [#7]
Oh OK, I used to used "banks" in my assembly days as there was nothing else! Also malloc in C. I guess I should have read the help. Thanx anyway!