PDA

View Full Version : Function_ReturnValue/ Function_ResultPtr



ReneMiner
17-09-2015, 12:15
we all know the case when a function has to return some value :D

The memory for that value is already allocated somewhere when we write



Function myFun() As <Result>

' where <Result> is the type of the functions return-value

' data as <Result> gets assigned within the function alike:

Function = 1
' but how can it be used within the function?


if Function = 1 Then
' this will work only because "Function" is followed by an equal-sign here
Endif

Function = Function + 1 ' this won't work... :(

End Function


it's not possible to request/use the current functions return-value in other cases, so we have to store it to some other local variable for further calculations, which is a waste of time & memory since the value is already present and stored somewhere.

If we knew the current functions return-values memory-position ("Function_ResultPtr") we could use a layover and alter single subelements if the function-result-type where an udt or if we had a built-in Function that tells us the current Function_ReturnValue then we could use it for further calculations without having to create an additional local variable...




Function myFun1() As t_XY

Local RetVal As t_XY At Function_ResultPtr

RetVal.X = 1
RetVal.Y = 234

End Function

Function myFun2() As String

Function = "Hello "
Function = Function_ReturnValue & "world!"

End Function

Function myFun3() As Long

Poke(Long, Function_ResultPtr, 123)
Function = Function_ReturnValue * 456

End Function

ErosOlmi
18-09-2015, 12:13
Have you thought to some parameters passed BYREF ?
Passing BYREF let you just have any function cascading all referencing the same variable/memory area.

ReneMiner
18-09-2015, 13:04
yes i thought about it.

The user then still has to create local data sometimes, especially if udt-subelements shall be passed ByRef he needs at least a local layover.
Also Byref returned Function-results are not instantly useable on the same line of code, while a function-return-value can be inserted via the call as a parameter within another call - else one would have to store the first result in a local variable temporary.

And this is mostly the intention behind this idea - have plain code and avoid to allocate additional memory if possible, to speed things up.