Page 1 of 1

[SOLVED] ProcedureReturn

Posted: Sat May 11, 2024 11:26 pm
by boddhi
Hello,

Can someone explain to me why this code returns 1 instead 0?
(I tried to find some topic about this but too much collected results with keyword ProcedureReturn)

Code: Select all

Procedure.a Test()
  Protected.a ReturnValue,Test
  
  If Not Test:ProcedureReturn:EndIf
  Debug "Test1"
  Debug "Test2"
  ProcedureReturn ReturnValue
EndProcedure

ReturnValue.a=test()

Debug ReturnValue

Code: Select all

If Not Test:ProcedureReturn:EndIf
 
As there is no value after ProcedureReturn, I expected 0 to be returned.
This is the first time I've been faced with this situation in 20 years.... :shock:

Re: ProcedureReturn

Posted: Sat May 11, 2024 11:31 pm
by Mindphazer
That's weird. On MacOS, your code returns 0 as expected (PB 6.10)
But on Windows, it returns 1 (PB 6.10 too)

Re: ProcedureReturn

Posted: Sat May 11, 2024 11:35 pm
by r-i-v-e-r
From the documentation:
https://www.purebasic.com/documentation/reference/procedures.html wrote: If no value is specified for ProcedureReturn, the returned value will be undefined (see inline assembly for more information).
This is an important behaviour meant to ease usage of inline assembly for the ASM backend, where the content of EAX/RAX is used accordingly as return value.

Without this, you'd have to MOV your result from ASM into a PB variable and then return the variable separately, which is inconvenient and a bit wasteful if it's already present in EAX/RAX.
Mindphazer wrote: Sat May 11, 2024 11:31 pm That's weird. On MacOS, your code returns 0 as expected (PB 6.10)
But on Windows, it returns 1 (PB 6.10 too)
For the C-backend it will return a zero-value instead, so this is indeed a source of variation between backends, but is allowed per the "undefined behaviour" disclaimer.

Re: ProcedureReturn

Posted: Sun May 12, 2024 12:50 am
by juergenkulow

Code: Select all

; If Not Test:ProcedureReturn:EndIf
  CMP    byte [rsp+48],0
  JE     No0
  XOR    rax,rax
  JMP    Ok0
No0:
  MOV    rax,1
Ok0:
  AND    rax,rax
  JE    _EndIf2
  JMP   _EndProcedure1
_EndIf2:

Code: Select all

// If Not Test:ProcedureReturn:EndIf
if (!(!(v_test))) { goto no2; }
r=0;
goto end;
no2:;

Re: ProcedureReturn

Posted: Sun May 12, 2024 1:41 am
by boddhi
@r-i-v-e-r
Thanks for your explanation.
And now that you say it, I actually have a vague memory of having read this somewhere a very very long time ago (C-backend didn't exist yet!).
Bad news, I will have to make sure on all my codes that there are no hidden defects in them. :( :)

@juergenkulow
Cool, but I don't understand anything in assembler coding! :wink: