exit sub ,exit function
exit sub ,exit function
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.
Re: exit sub ,exit function
Since my "crystal ball" is being "serviced" right now ...
Re: exit sub ,exit function
'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.
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.
Re: exit sub ,exit function
There nothing to see in a crystal ball after the 20th anyway. It goes blank.
Re: exit sub ,exit function
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
Re: exit sub ,exit function
From FB help (emphasis mine)greenink wrote:Exit sub is probably the same as return. Exit function leaves the function with the result values undefined? Or something else.
If you do NOT spec the return value it will default to zero.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.
Re: exit sub ,exit function
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:
Example of unexpected return value inducing a violent crash:
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
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
Re: exit sub ,exit function
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.
I already learnt my lesson about assuming when I was using the var keyword too much.
Re: exit sub ,exit function
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.
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.
Re: exit sub ,exit function
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.
See all control flow statements (CatPgControlFlow) to be taken into account.