Feature Request - ByRef

Blitz3D Forums/Blitz3D Programming/Feature Request - ByRef

Mikeyj21(Posted 2005) [#1]
Sorry if this is the wrong place for this, but I couldn't find any other!

Please, please, please... could we have the ability to 'pass by reference' to functions?

I promise to go away and never make any more requests if you grant this one!


Ross C(Posted 2005) [#2]
How do you mean, by reference?


Tom(Posted 2005) [#3]
edit: doh!

thought this was max :P


Ross C(Posted 2005) [#4]
Edit: nothing to see!


poopla(Posted 2005) [#5]
By reference, meaning that you would pass a "copy" of the variable to a function, which would then be edited. Any changes made to this variable effect the original, and all other references of the original.


Mikeyj21(Posted 2005) [#6]
Yep, pretty much as Dev said (except I believe that instead of a 'copy' being passed then edited, the memory address where the variable is held is passed).
Any changes made to the variable within the function are therefore actually made in the original. This has many benefits: two that immediately spring to mind are that you no longer have to use global variables, thus improving coding style by introducing variable 'scope', and if you pass a variable by reference (even if you don't intend to actually change it), then the memory and speed overhead of making a copy (which can be relatively large if the variable is a large instance of a type) is saved.

Basically, it is a feature which most modern languages implement - certainly Visual Basic, C++ and Java.


fall_x(Posted 2005) [#7]
(which can be relatively large if the variable is a large instance of a type)

I think you're wrong here, I'm pretty sure that type instances are always passed by reference, and other variables are passed by value.

Because if you do Type_Var.MyType=New MyType and then pass this on to a function, and change one of its fields there, it will have changed outside the function as well.

So maybe you could have types byRefString, byRefInt and byRefFloat which each contain one field, and pass those to your functions in order to mimic byref?


DJWoodgate(Posted 2005) [#8]
Well everything is passed by value and I can not see that changing. If you want a more advanced compiler use Bmax and wait for the inevitable 3d module(s).

However types are accessed by pointers and so are banks. So although you can not change what they point to when passed as an argument, you can certainly change the data they point to.

You can also use Blitz Arrays...
Local b#[0]
b[0]=1.03
print b[0]
change(b)
print b[0]
waitkey()

Function change(a#[0])
a[0]=a[0]+1
End Function

Though of course currently only one dimension and of constant size which I guess limits their flexibility a bit but maybe useful for vector and matrix calculations and so on where you do know the size of the array in advance.

vis.
; create a matrix to rotate points by angle theta around a given axis
Function MakeAxialRotMat(axis#[2],theta#,Mat#[8])
Local ax#=axis[0], ay#=axis[1], az#=axis[2], c#=Cos(theta), s#=Sin(theta)
Mat[0] = c+ax*ax*(1-c) : Mat[1] = ax*ay*(1-c)-az*s : Mat[2] = ax*az*(1-c)+ay*s
Mat[3] = ax*ay*(1-c)+az*s : Mat[4] = c+ay*ay*(1-c) : Mat[5] = ay*az*(1-c)-ax*s
Mat[6] = ax*az*(1-c)-ay*s : Mat[7] = ay*az*(1-c)+ax*s : Mat[8] = c+az*az*(1-c)
End Function


P.S. Fall_x please stop calling floats doubles, you will only raise false expectations and yet another feature request :0


fall_x(Posted 2005) [#9]
This is a quick work around for byref, only for strings but you should be able to add ints and floats quickly.

Type byRefStr
	Field val$
End Type

Function createString.byRefStr (val$)
	s.byRefStr=New byRefStr
	s\val$=val$
	Return s
End Function


; just a function to test passing a byref string and changing it :
Function testChangeIt(s.byRefStr)
	s\val$="After"
End Function


; using it :

s.byRefStr = createString ("Before")

Print s\val$

testChangeIt(s)

Print s\val$



fall_x(Posted 2005) [#10]
P.S. Fall_x please stop calling floats doubles, you will only raise false expectations and yet another feature request :0

sorry, my bad, got confused, I'll edit my posts :)


poopla(Posted 2005) [#11]
Well, that's why I said "copy" mike :). Alot of the people here don't know much about memory management I'm guessing.


Mikeyj21(Posted 2005) [#12]
fall_x: You are completely correct.... when I read the reference regarding functions, it states "...each parameter may be given an optional type tag. Parameters are always local.".

So... I mistakenly assumed that all parameters passed to functions were local! ;)

So, thanks VERY much for pointing this out, fall_x... I am a very happy bunny now! I can get back to a degree of good coding style!


In case anyone reading this want's further clarification(bear in mind I am new to Blitz, but definitely NOT new to programming!)... and in light of fall_x's post:

When a function it is called, variable(s) can be passed to the function (called 'parameters' by some, 'arguments' by others).
In most modern languages there are either two or three ways of passing these arguments..
1) By Value
2) By Reference
3) Pointers

As Pointers and 'By Reference' are very similar in function(C++ supports both, for example, but these days using pointers is considered a legacy of the old 'C' language (not that I am saying they are a bad thing!)).. I shall treat them as the same thing, leaving us with TWO ways of passing arguments.

When a variable is passed 'By Copy', an actual copy is made of the variable within the called function... any changes made to this variable within the function are lost when the function is exited.

However, when a variable is passed 'By Reference', the actual address of the memory containing the variable is passed to the function, and any modifications made within the function affect the variable in the calling function (simply put, it IS the same variable).

In Blitz3d, all basic variables (%,# and $) are passed 'By Copy', but (as fall_x has pointed out to me) variables which are custom types are passed 'By Reference' (a revelation which has made me absolutely overjoyed! But then I always was easily pleased :)

Hope this helps anyone who was struggling to understand what we were talking about (it would have been me, many moons ago......)