Page 1 of 1

Posted: Wed Jun 19, 2002 10:40 am
by BackupUser
Restored from previous forum. Originally posted by PB.

What is the best way to branch to another section of code from within a procedure?
You can't use Gosub to leave the procedure, and if the section you're going to
has procedure declarations, then you get an error about not having procedures
inside each other. Is there a way to do it? Thanks...

Or, how about this: do you need to use FakeReturn when using Goto to jump out
of a procedure?


PB - Registered PureBasic Coder

Edited by - PB on 19 June 2002 13:27:41

Posted: Wed Jun 19, 2002 4:12 pm
by BackupUser
Restored from previous forum. Originally posted by Franco.

Hi PB,
I can show you what I use:

Code: Select all

Global Value.s
Procedure TestBranch()
  Value = "This is a Value from the first Procedure!"
 
  MessageRequester("You are going to the ProcedureBranch...",Value,0)
 
  GotoLabel.l = ?ProcedureBranch
  CALL GotoLabel
  
  MessageRequester("Congratulations, you are back!",Value,0)
  
EndProcedure
 
Procedure NewProcedure()
  MessageRequester("Congratulations you are in the second Procedure!",Value,0)
EndProcedure
 
TestBranch()
 
End
 
ProcedureBranch:
  MessageRequester("This is the ProcedureBranch!",Value,0)
  NewProcedure()
Return
 
or if you don't like Global Variables:

Code: Select all

Procedure TestBranch()
  Dim Value.s(0) 
  Value(0) = "This is a Value from the first Procedure!"
 
  MessageRequester("You are going to the ProcedureBranch...",Value(0),0)
 
  GotoLabel.l = ?ProcedureBranch
  CALL GotoLabel
  
  MessageRequester("Congratulations, you are back!",Value(0),0)
  
EndProcedure
 
Procedure NewProcedure()
  MessageRequester("Congratulations you are in the second Procedure!",Value(0),0)
EndProcedure
 
TestBranch()
 
End
 
ProcedureBranch:
  MessageRequester("This is the ProcedureBranch!",Value(0),0)
  NewProcedure()
Return
 

Don't forget to enable inline ASM.


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Posted: Thu Jun 20, 2002 1:41 am
by BackupUser
Restored from previous forum. Originally posted by PB.

Thanks for your answer, Franco, but I'm not jumping to another procedure from
the calling procedure, but rather just to a label of code. Is there anything
wrong (crash-wise) about doing something like this:

Code: Select all

Procedure Commmand1_Click()
  Goto NewRoutine
EndProcedure
;
End
;
NewRoutine:
a=a+1
End

PB - Registered PureBasic Coder

Posted: Thu Jun 20, 2002 5:00 am
by BackupUser
Restored from previous forum. Originally posted by Franco.

It seems to work, but only if you don't jump back with Return like:

Code: Select all

Procedure Commmand1_Click()
  Goto NewRoutine
  ;do something else...
  ;bla, bla, bla...
EndProcedure
;
Commmand1_Click()
;
End
;
NewRoutine:
a=a+1
Return
This code crashes on Win98 and WinXP!



Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Posted: Thu Jun 20, 2002 6:55 am
by BackupUser
Restored from previous forum. Originally posted by Danilo.

GOTO / JMP (ASM) is a NO-Return operation.
GOSUB / CALL (ASM) is with Return.

cya,
...Danilo

(registered PureBasic user)

Posted: Thu Jun 20, 2002 7:20 am
by BackupUser
Restored from previous forum. Originally posted by PB.

> It seems to work, but only if you don't jump back with Return

Yep, Return is only to be used with Gosub anyway. Thanks!

PB - Registered PureBasic Coder

Posted: Thu Jun 20, 2002 3:48 pm
by BackupUser
Restored from previous forum. Originally posted by Franco.
Yep, Return is only to be used with Gosub anyway. Thanks!
But GoSub is not allowed inside Procedures...
So inline ASM is necessary if you need to return.


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Posted: Thu Jun 20, 2002 4:00 pm
by BackupUser
Restored from previous forum. Originally posted by Franco.
GOTO / JMP (ASM) is a NO-Return operation.
So GOTO is an implementation of JMP without adding any fancy stuff.

It would have been nice to have mentioned in the docs that there is no return possibility, that you are stucked where you jump...


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.