exit sub ,exit function

Forum for discussion about the documentation project.
Post Reply
greenink
Posts: 200
Joined: Jan 28, 2016 15:45

exit sub ,exit function

Post by greenink »

Exit sub is probably the same as return. Exit function leaves the function with the result values undefined? Or something else.
Last edited by greenink on Dec 19, 2016 12:59, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: exit sub ,exit function

Post by MrSwiss »

Since my "crystal ball" is being "serviced" right now ...
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: exit sub ,exit function

Post by fxm »

'Exit Sub/Function' requests to leave the code body of the sub/function (as if 'End Sub/Function' was encountered instead).

For a function, if the return value is previously initialized in the code, by for example 'Function = x', 'Exit' does not cancel/override this preset return value.
greenink
Posts: 200
Joined: Jan 28, 2016 15:45

Re: exit sub ,exit function

Post by greenink »

There nothing to see in a crystal ball after the 20th anyway. It goes blank.
greenink
Posts: 200
Joined: Jan 28, 2016 15:45

Re: exit sub ,exit function

Post by greenink »

Code: Select all

function orange(x as single) as single
   if(x=0) then 
      function=1
   else 
		exit function
   end if
end function
print orange(1)
getkey
Returns zero, so it makes an effort to supply a default value, perhaps?
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: exit sub ,exit function

Post by integer »

greenink wrote:Exit sub is probably the same as return. Exit function leaves the function with the result values undefined? Or something else.
From FB help (emphasis mine)
Return (from procedure) is used inside a procedure to exit the procedure possibly with a return value. A Sub cannot specify a return return value. In a Function, Return must specify its return value. Return expression is roughly equivalent to the Function = expression : Exit Function idiom.
If you do NOT spec the return value it will default to zero.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: exit sub ,exit function

Post by fxm »

Whatever the output branch used, the return value must be always defined, otherwise an unexpected value may be returned.
Because without return value initialization, memory for return value is well allocated normally, but the implicit constructor (for object) is not called after.

Example of unexpected return value:

Code: Select all

Type UDT
  Dim As Integer I = 123
End Type

Function f () As UDT
  If 1 = 2 Then Return Type<UDT>()
End Function

Print Type<UDT>().I, f().I

Sleep

Code: Select all

 123           4199925
Example of unexpected return value inducing a violent crash:

Code: Select all

Type UDT
  Dim As String S = "123"
End Type

Function f () As UDT
  If 1 = 2 Then Return Type<UDT>()
End Function

Print Type<UDT>().S, f().S

Sleep
greenink
Posts: 200
Joined: Jan 28, 2016 15:45

Re: exit sub ,exit function

Post by greenink »

Better safe than sorry in such a case. The compiler will complain about no return value being set in simpler cases where it can check.
I already learnt my lesson about assuming when I was using the var keyword too much.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: exit sub ,exit function

Post by sancho2 »

The complier gives out a warning for "Function not explicitly set" when there is no return in it.
Shouldn't the compiler warn me if all the paths to the return are hidden behind if statements?
Or maybe a better way to phrase is to say that there should be a warning if there is a possible path to the end of the function that doesn't explicitly set a return value.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: exit sub ,exit function

Post by fxm »

I think testing all the possible branches would be quite complex for the compiler.
See all control flow statements (CatPgControlFlow) to be taken into account.
Post Reply