fxm, I may have misinterpreted your post/posts. So please bear with me here.
fxm wrote:test_b() as test_d() returns a reference.
By specifying Return Byval, an address (usually stored in a pointer) can be passed directly as-is, forcing the Byref function result to reference the same memory location which the address pointed to.
I don't understand why the function could point to sth. else. If I return
*px then the byref-return would make a
@(*px) out of it and then dereference it "upon arrival". Using
byval px (I feel like) does exactly the same thing. In what scenario could the returned pointer (internally) point to a different address?
I've also had a look at the linked thread, from which I draw the same conclusion.
Code: Select all
sub FAKEPRINT1(byref E as integer)
Print E
end sub
dim as integer e = 1
FAKEPRINT1 (e)
FAKEPRINT1 (*(@e))
FAKEPRINT1 (byval @e)
sleep
Output:
The only difference I saw was the way the parser handled it in a function call:
Code: Select all
function f(byref i as integer) byref as integer
print "inside=";@i
f = i
end function
dim as integer i=123
print "i from outside=";@i
Print i
f(i) = 100 'parsed as equality operator, not assignement
print i
(f(i)) = 200
print i
f(byval @i) = 300
print i
sleep
Output:
Code: Select all
i from outside=1638080
123
inside=1638076
123
inside=1638080
200
inside=1638080
300
It's interesting to see a different address in the first function call. It seems like a temporary copy is made from the variable
i.