Undefined ProcedureReturn should be #False (0)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
es_91
Enthusiast
Enthusiast
Posts: 242
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

Undefined ProcedureReturn should be #False (0)

Post 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.
Last edited by es_91 on Tue May 22, 2018 9:13 pm, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Undefined ProcedureReturn should be #False (0)

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
es_91
Enthusiast
Enthusiast
Posts: 242
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

Re: Undefined ProcedureReturn should be #False (0)

Post by es_91 »

So, it surely never becomes 1/#True? OK. That's enough. 8)
es_91
Enthusiast
Enthusiast
Posts: 242
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

Re: Undefined ProcedureReturn should be #False (0)

Post by es_91 »

Oh, well, it does become 1. That is... i don't know... :(
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Undefined ProcedureReturn should be #False (0)

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Undefined ProcedureReturn should be #False (0)

Post 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 ?
Windows (x64)
Raspberry Pi OS (Arm64)
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Undefined ProcedureReturn should be #False (0)

Post 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
uweb
User
User
Posts: 98
Joined: Wed Mar 15, 2006 9:40 am
Location: Germany

Re: Undefined ProcedureReturn should be #False (0)

Post 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.
Please pardon my English, my native tongue is German.
Post Reply