[SOLVED] ProcedureReturn

Just starting out? Need help? Post your questions and find answers here.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

[SOLVED] ProcedureReturn

Post 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:
Last edited by boddhi on Sun May 12, 2024 1:42 am, edited 1 time in total.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: ProcedureReturn

Post 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)
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
r-i-v-e-r
User
User
Posts: 20
Joined: Thu May 09, 2024 5:18 pm

Re: ProcedureReturn

Post 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.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: ProcedureReturn

Post 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:;
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: ProcedureReturn

Post 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:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Post Reply