Exit from Procedure

Just starting out? Need help? Post your questions and find answers here.
Capella
User
User
Posts: 16
Joined: Wed Mar 19, 2008 5:32 pm

Exit from Procedure

Post 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.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post 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

Capella
User
User
Posts: 16
Joined: Wed Mar 19, 2008 5:32 pm

More than one ProcedureReturn

Post by Capella »

Ok, thanks.

So this mean that it's possible to have more than one (1) line with ProcedureReturn in a subroutine.
Seymour Clufley
Addict
Addict
Posts: 1267
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Post 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.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post 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")

Post Reply