exit sub ,exit function

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

exit sub ,exit function

Postby greenink » Dec 19, 2016 4:02

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: 2811
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: exit sub ,exit function

Postby MrSwiss » Dec 19, 2016 11:59

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

Re: exit sub ,exit function

Postby fxm » Dec 19, 2016 12:54

'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: 199
Joined: Jan 28, 2016 15:45

Re: exit sub ,exit function

Postby greenink » Dec 19, 2016 12:58

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

Re: exit sub ,exit function

Postby greenink » Dec 19, 2016 13:07

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: 373
Joined: Feb 01, 2007 16:54
Location: usa

Re: exit sub ,exit function

Postby integer » Dec 19, 2016 13:27

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
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: exit sub ,exit function

Postby fxm » Dec 19, 2016 13:31

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: 199
Joined: Jan 28, 2016 15:45

Re: exit sub ,exit function

Postby greenink » Dec 19, 2016 16:01

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

Postby sancho2 » Aug 26, 2017 8:41

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
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: exit sub ,exit function

Postby fxm » Aug 26, 2017 9:43

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

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 2 guests