Page 1 of 1

Undefined ProcedureReturn should be #False (0)

Posted: Tue May 22, 2018 9:04 pm
by es_91
The undefined return of a procedure is, practically, a random number.

It could be zero, i think it should.

However, if it is undefined, it could become 1 that would interfere with a situation where i test the function for success but the success was never set.

Code: Select all

Procedure function ()
	
	ProcedureReturn
	
EndProcedure

If function ()
	; should only be reached when return is set ProcedureReturn 1
	; is reached nevertheless
	Debug "hello"
EndIf

If function () = #True
	; COULD equally be reached in few cases; should not be reacheable
	Debug "HELLO AGAIN; WHAT A MISTAKE ;-)"
EndIf
But it should be zero.

Re: Undefined ProcedureReturn should be #False (0)

Posted: Tue May 22, 2018 9:13 pm
by wilbert
es_91 wrote:The undefined return of a procedure is, practically, a random number.

It could be zero, i think it should.

However, if it is undefined, it could become 1 that would interfere with a situation where i test

Code: Select all

if function () : ;only do when return is set with ProcedureReturn 1
endif
But it should be zero.
It is certainly not a random number.
ProcedureReturn without an argument returns the value of the cpu register eax / rax.
It’s very important it stays this way.

Re: Undefined ProcedureReturn should be #False (0)

Posted: Tue May 22, 2018 9:14 pm
by es_91
So, it surely never becomes 1/#True? OK. That's enough. 8)

Re: Undefined ProcedureReturn should be #False (0)

Posted: Tue May 22, 2018 9:19 pm
by es_91
Oh, well, it does become 1. That is... i don't know... :(

Re: Undefined ProcedureReturn should be #False (0)

Posted: Wed May 23, 2018 2:53 am
by TI-994A
es_91 wrote:The undefined return of a procedure is, practically, a random number.

It could be zero, i think it should.

However, if it is undefined, it could become 1 that would interfere with a situation where i test the function for success but the success was never set...
This is a perfect scenario to illustrate the importance of good coding practices. While not always adhered to, and sometimes even debatable, procedures should invariably contain only one single return.

Consider such a model:

Code: Select all

Procedure function ()
  result = 0

  If something
    result = 1
  ElseIf something_else
    result = 2
  EndIf

  If result > 0
    ;further conditional processing
  EndIf

  ProcedureReturn result
EndProcedure
It's never a good idea to depend on compiler defaults; always assign required values explicitly.

Re: Undefined ProcedureReturn should be #False (0)

Posted: Wed May 23, 2018 4:49 am
by wilbert
es_91 wrote:Oh, well, it does become 1. That is... i don't know... :(
It depends on the assembly code PB generates.
If the value of eax/rax happens to be 1, ProcedureReturn will return a 1.
Why don't you simple write ProcedureReturn 0 instead of ProcedureReturn ?

Re: Undefined ProcedureReturn should be #False (0)

Posted: Wed May 23, 2018 8:32 am
by walbus
When writing functions which others should use, it is very important to always return a defined value

I myself always hold it that way:
1 OK
0 No error, but also no successful execution
-1, -2, -3 Error from a previously defined table

Re: Undefined ProcedureReturn should be #False (0)

Posted: Tue May 29, 2018 10:38 am
by uweb
Yes, but may it would be better to have on Standard for all - not only ProcedureReturn.
e.g. Cool() instead of Bool() handles three cases (#True, #False and #NIL) instead of two.