English Amiga Board

English Amiga Board (http://eab.abime.net/index.php)
-   Coders. AMOS (http://eab.abime.net/forumdisplay.php?f=119)
-   -   Jumping to a label in another procedure (http://eab.abime.net/showthread.php?t=102864)

Brick Nash 26 June 2020 10:30

Jumping to a label in another procedure
 
Is it possible to jump to a label within a procedure from another procedure? I've been trying to do this but with not much luck, and it'd be REALLY handy if it were possible.

For example, if Label_1 is inside Procedure 2 and Procedure 2 is called from inside Procedure 1:

Code:

Procedure 1

Goto Label_1
Proc Procedure 2

End Proc

The manual details on how to jump in and out of procedures using variable values, but not labels.

Samurai_Crow 26 June 2020 16:33

No. Not possible.

Exodous 26 June 2020 18:46

It's 20+ years since I last did any AMOS, but you could "sort of" do it by passing an argument to the second procedure and, if that argument is a set value go to that label as below.

Any usage of "PRC2" will need an argument so, if you don't want to jump to the label, use something like PRC2[0].

It's not particularly nice programming though. If you need the same code in more than one procedure then put it in it's own procedure and just call that procedure from where it is required, or just merge both procedures and use If Then statements.


Code:

' Run procedure PRC2 without jumping to the label
PRC2[0]

' Run procedure PRC1 which in turn runs PRC2 jumping to the label
PRC1


' The first procedure
Procedure PRC1

Print "In Proc1"

' Call the second procedure with a '1' to jump to the label section.
PRC2[1]

End Proc


' The second procedure - call this with a '1' to jump to the label, or anything else to not.
Procedure PRC2[ARG]
If ARG=1 Then Goto LABEL1

Print "Not jumping"
Goto PRC2END

LABEL1:
Print "Jumped to the label"

PRC2END:
End Proc


Brick Nash 01 July 2020 09:52

Wow that's interesting! Thanks for that Exodous. :)

roondar 01 July 2020 15:53

A different way of looking at this would be to split up the procedures into separate components. That saves the need for using goto's and is a good way to functionally split your program up into smaller components.

Such a method can help getting you smaller (sometimes even faster) and also easier to read/debug programs.

So, in this example you'd have Procedure 1, Procedure 2 and Procedure shared_1_2 (naming is for the example only, choose a name that fits what the procedures do ;))
Then, you'd call proc 1 or 2 and they'd call proc shared_1_2 as needed.

Brick Nash 02 July 2020 20:07

Quote:

Originally Posted by roondar (Post 1411121)
A different way of looking at this would be to split up the procedures into separate components. That saves the need for using goto's and is a good way to functionally split your program up into smaller components.

Such a method can help getting you smaller (sometimes even faster) and also easier to read/debug programs.

So, in this example you'd have Procedure 1, Procedure 2 and Procedure shared_1_2 (naming is for the example only, choose a name that fits what the procedures do ;))
Then, you'd call proc 1 or 2 and they'd call proc shared_1_2 as needed.

Yes I think that's the kind of thing I'm looking for. I love the fact that Procedures can be closed and compact to one line which is very tidy (I wish subroutines could do that), and I was trying to use them like subroutines but obviously they are a bit more detached than that.


All times are GMT +2. The time now is 15:15.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.

Page generated in 0.04580 seconds with 11 queries