Page 1 of 1

Exit from Procedure

Posted: Wed Apr 02, 2008 9:31 am
by Capella
What is the equivalent to "EXIT SUB", that is, how to exit from a subroutine before reaching the EndSubroutine. The "Break" command only exit from the current loop. And "Break #" (where # is the number of the nested loop) only exit from the that number of nested loops. I prefer not to use "Goto label", even if that would solve the problem.

Posted: Wed Apr 02, 2008 9:43 am
by Num3
Yes!

Code: Select all


For a=1 to 100000

If a=10
 Break ; exits the loop
Endif

Next

Code: Select all

Procedure DoSomething()

For a=1 to 100000

If a=10
 ProcedureReturn ; exits the procedure
Endif

Next
Endprocedure


More than one ProcedureReturn

Posted: Wed Apr 02, 2008 9:58 am
by Capella
Ok, thanks.

So this mean that it's possible to have more than one (1) line with ProcedureReturn in a subroutine.

Posted: Wed Apr 02, 2008 9:59 am
by Seymour Clufley
Yes.

Don't call them "subroutines". That will confuse people. In PureBasic, they are called procedures.

You can use ProcedureReturn to pass a variable out of the procedure.

ProcedureReturn "result"
ProcedureReturn 3

and so on.

Posted: Wed Apr 02, 2008 10:39 am
by Num3
Here's a more complex example:

Code: Select all

Procedure Morpheus(Answer.s)

If answer.s="blue pill"
 ProcedureReturn 1 ; returns 1 
Endif

If answer.s="red pill"
 ProcedureReturn 2
Endif

;If no answer return 0

 ProcedureReturn 0

Endprocedure


Debug Morpheus("blue pill")