Return [value]
Parameters
| value (optional) - the result to hand back to the caller; converted to the function's declared return type. Must be left off in the main program |
Description
|
Leaves a function, optionally handing back a value. Inside a Function, Return stops the function there and then and gives the caller a result: Return dx*dx+dy*dy. Nothing after it runs, which makes it handy for bailing out early - check the awkward cases at the top, Return, and let the rest of the function assume everything is fine. The value is converted to whatever the function's name says it returns, so a Return inside Function Distance#() comes back as a float even if you hand it an integer. A bare Return leaves with the default result for the type: 0, 0.0, "" or Null. Falling off the end at End Function does exactly the same thing. Return has a second job in the main program: it ends a subroutine that was called with Gosub and sends execution back to the line after it. Used that way it must not carry a value - the compiler rejects "Main program cannot return a value". Return leaves the whole function, however many loops deep it is. To leave just the loop you are in, use Exit. See also: Function, End Function, Gosub, Exit. |
Example
; Return Example ; -------------- ; Call our damage function for two different attacks Print "The goblin swings a rusty sword ..." Print " Damage dealt: "+Damage(6,8) Print "The knight swings an enchanted axe ..." Print " Damage dealt: "+Damage(25,8) Print "" Print "Press any key to close the example" WaitKey End Function Damage(base,armour) ; Return ends the function at once and hands a value back to the caller If base<=armour Then ; A glancing blow - nothing below this Return runs Return 1 End If Return base-armour End Function
Index