Gosub Question
Archiver
Posts: 46,084
Can someone explain the advantages or disadvantages to using GOSUB Vs GOTO?
My program seems to lose direction and get lost and I think it is because I
might be mis using these commands.
Thanks
Tim
[noparse][[/noparse]Non-text portions of this message have been removed]
My program seems to lose direction and get lost and I think it is because I
might be mis using these commands.
Thanks
Tim
[noparse][[/noparse]Non-text portions of this message have been removed]
Comments
If there is some task that (SAME TASK) needs to be repeated at many DIFFERENT
places within the program I use a gosub.....that way you write code for the
routine only once...(ex: turn on led one, turn off led two, and activate relay
one, and deactivate relay 2 )
As you may already know, when the gosub is finished, the next line after the
gosub instruction is executed.
I use GOTO when I want to branch to some unique part of the program.....
This is probably a crappy explanation.....I am sure some better explanations
will follow.
ken
Can someone explain the advantages or disadvantages to using GOSUB Vs GOTO?
My program seems to lose direction and get lost and I think it is because I
might be mis using these commands.
Thanks
Tim
[noparse][[/noparse]Non-text portions of this message have been removed]
I haven't been into programming for quite a while but I remember using
subroutines, (gosubs), when the same task was used by several different
parts of a program. For example, there could be several different places in
a program where hex data is collected but you would like to have the data
converted to decimal - a subroutine could do this for you without repeating
the conversion process every time you got the hex data.
A "goto" is an unconditional jump to a different part of the program.
Does this help??
Bob
Original Message
From: Trkeenan@a... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=cYb6_iW3AC5m4sOO830QHEfwDMTogt4bm8DKPetYL_04er0_-TuU_QfL0ZraTpHwykVbjrFoTQs]Trkeenan@a...[/url
Sent: Thursday, April 15, 2004 8:34 PM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] Gosub Question
Can someone explain the advantages or disadvantages to using GOSUB Vs GOTO?
My program seems to lose direction and get lost and I think it is because I
might be mis using these commands.
Thanks
Tim
[noparse][[/noparse]Non-text portions of this message have been removed]
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject and
Body of the message will be ignored.
Yahoo! Groups Links
One robotics company I had worked for a few years back
had a program they wanted expanded.
When I started working on program, I realized it had been
modified by at least five other programmers. Every time a programmer
made changes, he used hundreds of GOTO statements.
It was a plethora of madness, with thousands of GOTOs all over
and NO documentation or programming structure.
Now, if these programmers had used a more structured
programming technique, like writing subroutines and
using GOSUBs, the program would be much more easy
to expand, modify, and understand, no matter how many
programmers worked on it.
There are numerous examples in tutorials about using this
command. Maybe this very general flow example will help.
MAIN PROGRAM
GOSUB WORK 1
MORE PROGRAM
GOSUB WORK 2
MORE PROGRAM
END
WORK 1
RETURN
WORK 2
RETURN
Some engineering programming languages may not have the
GOSUB statement. Then you'll need GOTOs. Sticking to the subroutine
structure mentioned will help. And always include documentation
in the program. But don't worry, all Basic Stamps have
GOSUB/RETURN, including the Basic Stamp 1.
I hope this helps.
-Mike
--- In basicstamps@yahoogroups.com, Trkeenan@a... wrote:
> Can someone explain the advantages or disadvantages to using GOSUB
Vs GOTO?
> Tim
also say you want the robot to move forward an exact amount, turn left, move the
same amount it did before and keep this pattern until the robot ends up where it
started. The code with NO GOSUBs would look something like this:
'{$STAMP BS2}
a VAR word
leftmotor CON 12
rightmotor CON 13
LOW leftmotor
LOW rightmotor
start:
FOR a = 1 TO 100
PULSOUT leftmotor,1000
PULSOUT rightmotor,500
PAUSE 20
next
FOR a = 1 TO 25
PULSOUT leftmotor,500
PULSOUT rightmotor,500
PAUSE 20
next
FOR a = 1 TO 100
PULSOUT leftmotor,1000
PULSOUT rightmotor,500
PAUSE 20
next
FOR a = 1 TO 25
PULSOUT leftmotor,500
PULSOUT rightmotor,500
PAUSE 20
next
FOR a = 1 TO 100
PULSOUT leftmotor,1000
PULSOUT rightmotor,500
PAUSE 20
next
FOR a = 1 TO 25
PULSOUT leftmotor,500
PULSOUT rightmotor,500
PAUSE 20
next
FOR a = 1 TO 100
PULSOUT leftmotor,1000
PULSOUT rightmotor,500
PAUSE 20
next
stop
This will make the same robot behave the same way WITH GOSUBs:
'{$STAMP BS2}
a VAR word
leftmotor CON 12
rightmotor CON 13
LOW leftmotor
LOW rightmotor
start:
GOSUB forward
GOSUB left
GOSUB forward
GOSUB left
GOSUB forward
GOSUB left
GOSUB forward
Stop
left:
FOR a = 1 TO 25
PULSOUT leftmotor,500
PULSOUT rightmotor,500
PAUSE 20
next
return
forward:
FOR a = 1 TO 100
PULSOUT leftmotor,1000
PULSOUT rightmotor,500
PAUSE 20
next
return
That's 37 lines of code WITH GOSUBs and 43 lines WITHOUT GOSUBs to make it do
the same thing. This explains the importance of gosubs. Now a little more back
onto the topic of difference between GOSUB and GOTO...These are 2 very different
comands. The GOSUB command will call a subroutine that starts with a label and
ends with a RETURN. This subroutine can be used at any time during the program.
The GOTO command will GO TO the label, and cannot return to where it came
without another goto, but say you have 2 different parts of the program and you
want to do the same thing. The goto command simply will not work in this case.
John Baker
http://www.geocities.com/johnsrobotics/
Trkeenan@a... wrote:
Can someone explain the advantages or disadvantages to using GOSUB Vs GOTO?
My program seems to lose direction and get lost and I think it is because I
might be mis using these commands.
Thanks
Tim
[noparse][[/noparse]Non-text portions of this message have been removed]
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject and Body of
the message will be ignored.
Yahoo! Groups Links
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
[noparse][[/noparse]Non-text portions of this message have been removed]
mikegotis@y... writes:
One robotics company I had worked for a few years back
had a program they wanted expanded.
When I started working on program, I realized it had been
modified by at least five other programmers. Every time a programmer
made changes, he used hundreds of GOTO statements.
It was a plethora of madness, with thousands of GOTOs all over
and NO documentation or programming structure.
Now, if these programmers had used a more structured
programming technique, like writing subroutines and
using GOSUBs, the program would be much more easy
to expand, modify, and understand, no matter how many
programmers worked on it.
There are numerous examples in tutorials about using this
command. Maybe this very general flow example will help.
MAIN PROGRAM
GOSUB WORK 1
MORE PROGRAM
GOSUB WORK 2
MORE PROGRAM
END
WORK 1
RETURN
WORK 2
RETURN
Some engineering programming languages may not have the
GOSUB statement. Then you'll need GOTOs. Sticking to the subroutine
structure mentioned will help. And always include documentation
in the program. But don't worry, all Basic Stamps have
GOSUB/RETURN, including the Basic Stamp 1.
I hope this helps.
-Mike
Thanks for the info.
Is there a chance when you do a gosub the return at the end of the routine
sends you to the wrong place in the program?
Tim
[noparse][[/noparse]Non-text portions of this message have been removed]
>In a message dated 4/15/2004 11:14:27 PM Mountain Standard Time,
>mikegotis@y... writes:
>One robotics company I had worked for a few years back
>had a program they wanted expanded.
>When I started working on program, I realized it had been
>modified by at least five other programmers. Every time a programmer
>made changes, he used hundreds of GOTO statements.
>It was a plethora of madness, with thousands of GOTOs all over
>and NO documentation or programming structure.
>
>Now, if these programmers had used a more structured
>programming technique, like writing subroutines and
>using GOSUBs, the program would be much more easy
>to expand, modify, and understand, no matter how many
>programmers worked on it.
>
>There are numerous examples in tutorials about using this
>command. Maybe this very general flow example will help.
>
>MAIN PROGRAM
>
>GOSUB WORK 1
>
>MORE PROGRAM
>
>GOSUB WORK 2
>
>MORE PROGRAM
>
>END
>
>WORK 1
>
>RETURN
>
>WORK 2
>
>RETURN
>
>Some engineering programming languages may not have the
>GOSUB statement. Then you'll need GOTOs. Sticking to the subroutine
>structure mentioned will help. And always include documentation
>in the program. But don't worry, all Basic Stamps have
>GOSUB/RETURN, including the Basic Stamp 1.
>
>I hope this helps.
>
>-Mike
>Thanks for the info.
>
> Is there a chance when you do a gosub the return at the end of the routine
>sends you to the wrong place in the program?
>
>Tim
Tim -
This can not happen inadvertently. In other words, so long as there are
logically the same number of GOSUBS and RETURNS, you will always return to the
place from which you left. HOWEVER, if there are NOT an equal number of GOSUB
and RETURN statements, from a program logic point of view, or if the RETURN
stack is altered, then a spurious RETURN is possible. One of the other things
that you must ensure is that you MUST NOT drop into a RETURN statement which has
no preceding GOSUB. Beware of places where there is a RETURN statement with a
label just preceding it. That is danger in the making!
A few things to remember when using GOSUB and RETURN. Always count to ensure
that you have not exceeded the maximum number of stacked (GOSUB within GOUSUB)
routines as specified by the compiler or interpreter being used. Also make sure,
if there is a limitation, that you have not exceeded the maximum number of GOSUB
statements in a program. For all PBASIC Stamps, there is a maximum stack depth
(GOSUB within GOSUB) of 4 levels. For the BS-1 there are a maximum of 16 GOSUBs
permitted per program. For all other Stamps, this maximum number of GOSUBs is
255.
I hope that is helpful, and not confusing.
Regards,
Bruce Bates
other Stamps are there?
And yes, you are only 'allowed' a certain number
of 'nested' GOSUB calls. That's where one subroutine
calls another, which then calls another.... When
you reach depth 5, the first 'return address' is lost.
Then, when you 'RETURN' the fifth time, a 'bogus'
return address is used and who knows where your
program goes.
GOSUB has the advantage that, unless you exceed
the nesting, it ALWAYS returns to the place which
called it when you say 'RETURN'.
--- In basicstamps@yahoogroups.com, Bruce Bates <bvbates@u...> wrote:
> At 07:03 AM 4/16/04 -0400, Trkeenan@a... wrote:
> >In a message dated 4/15/2004 11:14:27 PM Mountain Standard Time,
> >mikegotis@y... writes:
> >One robotics company I had worked for a few years back
> >had a program they wanted expanded.
> >When I started working on program, I realized it had been
> >modified by at least five other programmers. Every time a
programmer
> >made changes, he used hundreds of GOTO statements.
> >It was a plethora of madness, with thousands of GOTOs all over
> >and NO documentation or programming structure.
> >
> >Now, if these programmers had used a more structured
> >programming technique, like writing subroutines and
> >using GOSUBs, the program would be much more easy
> >to expand, modify, and understand, no matter how many
> >programmers worked on it.
> >
> >There are numerous examples in tutorials about using this
> >command. Maybe this very general flow example will help.
> >
> >MAIN PROGRAM
> >
> >GOSUB WORK 1
> >
> >MORE PROGRAM
> >
> >GOSUB WORK 2
> >
> >MORE PROGRAM
> >
> >END
> >
> >WORK 1
> >
> >RETURN
> >
> >WORK 2
> >
> >RETURN
> >
> >Some engineering programming languages may not have the
> >GOSUB statement. Then you'll need GOTOs. Sticking to the subroutine
> >structure mentioned will help. And always include documentation
> >in the program. But don't worry, all Basic Stamps have
> >GOSUB/RETURN, including the Basic Stamp 1.
> >
> >I hope this helps.
> >
> >-Mike
> >Thanks for the info.
> >
> > Is there a chance when you do a gosub the return at the end of
the routine
> >sends you to the wrong place in the program?
> >
> >Tim
>
> Tim -
>
> This can not happen inadvertently. In other words, so long as there
are logically the same number of GOSUBS and RETURNS, you will always
return to the place from which you left. HOWEVER, if there are NOT an
equal number of GOSUB and RETURN statements, from a program logic
point of view, or if the RETURN stack is altered, then a spurious
RETURN is possible. One of the other things that you must ensure is
that you MUST NOT drop into a RETURN statement which has no preceding
GOSUB. Beware of places where there is a RETURN statement with a
label just preceding it. That is danger in the making!
>
> A few things to remember when using GOSUB and RETURN. Always count
to ensure that you have not exceeded the maximum number of stacked
(GOSUB within GOUSUB) routines as specified by the compiler or
interpreter being used. Also make sure, if there is a limitation,
that you have not exceeded the maximum number of GOSUB statements in
a program. For all PBASIC Stamps, there is a maximum stack depth
(GOSUB within GOSUB) of 4 levels. For the BS-1 there are a maximum of
16 GOSUBs permitted per program. For all other Stamps, this maximum
number of GOSUBs is 255.
>
> I hope that is helpful, and not confusing.
>
> Regards,
>
> Bruce Bates
>I thought ALL stamps ran PBASIC -- what
>other Stamps are there?
The Javelin Stamp run a subset of Java.
>And yes, you are only 'allowed' a certain number
>of 'nested' GOSUB calls. That's where one subroutine
>calls another, which then calls another.... When
>you reach depth 5, the first 'return address' is lost.
>Then, when you 'RETURN' the fifth time, a 'bogus'
>return address is used and who knows where your
>program goes.
>
>GOSUB has the advantage that, unless you exceed
>the nesting, it ALWAYS returns to the place which
>called it when you say 'RETURN'.
>
>
>
>
>--- In basicstamps@yahoogroups.com, Bruce Bates <bvbates@u...> wrote:
>> At 07:03 AM 4/16/04 -0400, Trkeenan@a... wrote:
>> >In a message dated 4/15/2004 11:14:27 PM Mountain Standard Time,
>> >mikegotis@y... writes:
>> >One robotics company I had worked for a few years back
>> >had a program they wanted expanded.
>> >When I started working on program, I realized it had been
>> >modified by at least five other programmers. Every time a
>programmer
>> >made changes, he used hundreds of GOTO statements.
>> >It was a plethora of madness, with thousands of GOTOs all over
>> >and NO documentation or programming structure.
>> >
>> >Now, if these programmers had used a more structured
>> >programming technique, like writing subroutines and
>> >using GOSUBs, the program would be much more easy
>> >to expand, modify, and understand, no matter how many
>> >programmers worked on it.
>> >
>> >There are numerous examples in tutorials about using this
>> >command. Maybe this very general flow example will help.
>> >
>> >MAIN PROGRAM
>> >
>> >GOSUB WORK 1
>> >
>> >MORE PROGRAM
>> >
>> >GOSUB WORK 2
>> >
>> >MORE PROGRAM
>> >
>> >END
>> >
>> >WORK 1
>> >
>> >RETURN
>> >
>> >WORK 2
>> >
>> >RETURN
>> >
>> >Some engineering programming languages may not have the
>> >GOSUB statement. Then you'll need GOTOs. Sticking to the subroutine
>> >structure mentioned will help. And always include documentation
>> >in the program. But don't worry, all Basic Stamps have
>> >GOSUB/RETURN, including the Basic Stamp 1.
>> >
>> >I hope this helps.
>> >
>> >-Mike
>> >Thanks for the info.
>> >
>> > Is there a chance when you do a gosub the return at the end of
>the routine
>> >sends you to the wrong place in the program?
>> >
>> >Tim
>>
>> Tim -
>>
>> This can not happen inadvertently. In other words, so long as there
>are logically the same number of GOSUBS and RETURNS, you will always
>return to the place from which you left. HOWEVER, if there are NOT an
>equal number of GOSUB and RETURN statements, from a program logic
>point of view, or if the RETURN stack is altered, then a spurious
>RETURN is possible. One of the other things that you must ensure is
>that you MUST NOT drop into a RETURN statement which has no preceding
>GOSUB. Beware of places where there is a RETURN statement with a
>label just preceding it. That is danger in the making!
>>
>> A few things to remember when using GOSUB and RETURN. Always count
>to ensure that you have not exceeded the maximum number of stacked
>(GOSUB within GOUSUB) routines as specified by the compiler or
>interpreter being used. Also make sure, if there is a limitation,
>that you have not exceeded the maximum number of GOSUB statements in
>a program. For all PBASIC Stamps, there is a maximum stack depth
>(GOSUB within GOSUB) of 4 levels. For the BS-1 there are a maximum of
>16 GOSUBs permitted per program. For all other Stamps, this maximum
>number of GOSUBs is 255.
>>
>> I hope that is helpful, and not confusing.
>>
>> Regards,
>>
>> Bruce Bates
>
>
>
>To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
>from the same email address that you subscribed. Text in the Subject and Body
of the message will be ignored.
>
>Yahoo! Groups Links
>
>
>
>
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
Original Message
From: Allan Lane [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=lZeGtuBjbriTp2cWohpUhvmws4mssV60hMWQnBGaNUQf0S7DqC7CXOigjtiRQWQpeZ4q38wZD-ZgyRqIepMZTiNq7g]allan.lane@h...[/url
Sent: Friday, April 16, 2004 8:41 AM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] Re: Gosub Question
I thought ALL stamps ran PBASIC -- what
other Stamps are there?
<snip>
GOSUB, and someone said all PBASIC running Stamps
had a GOSUB level of 4, except those that had
a level of 255, and *I* then wanted to say
"what are THOSE that have 255?".
I should have asked: What Basic Stamp based
package has a call depth of 255?
I know the Javelin. I own a Javelin. I even
LIKE the Javelin. Javelin doesn't HAVE a GOSUB.
-- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
wrote:
> The Javelin Stamp runs a subset of the Sun Microsystems Java
language.
>
> -- Jon Williams
> -- Applications Engineer, Parallax
> -- Dallas Office
>
>
>
Original Message
> From: Allan Lane [noparse][[/noparse]mailto:allan.lane@h...]
> Sent: Friday, April 16, 2004 8:41 AM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] Re: Gosub Question
>
>
> I thought ALL stamps ran PBASIC -- what
> other Stamps are there?
>
> <snip>
>Right, right, My Bad -- we were all talking about
>GOSUB, and someone said all PBASIC running Stamps
>had a GOSUB level of 4, except those that had
>a level of 255, and *I* then wanted to say
>"what are THOSE that have 255?".
>
>I should have asked: What Basic Stamp based
>package has a call depth of 255?
>
>I know the Javelin. I own a Javelin. I even
>LIKE the Javelin. Javelin doesn't HAVE a GOSUB.
Alan -
That "someone" was me. Here is a direct repeat of what I said, just so there's
no misunderstanding. There are TWO separate and distinct elements to consider:
quote
Also make sure, if there is a limitation, that you have not exceeded the maximum
number of GOSUB statements in a program. For all PBASIC Stamps, there is a
maximum stack depth (GOSUB within GOSUB) of 4 levels. For the BS-1 there are a
maximum of 16 GOSUBs permitted per program. For all other Stamps, this maximum
number of GOSUBs is 255.
end quote
The first consideration is the DEPTH of stacking. The second consideration is
the absolute number of GOSUBs within the programs. The latter differs between
the BS-1 and other PBASIC Stamps (BS-2, BS-2SX, BS-2e, BS-2p), with that
difference noted above.
Regards,
Bruce Bates
>My program seems to lose direction and get lost and I think it is because I
>might be mis using these commands.
> Is there a chance when you do a gosub the return at the end of the routine
>sends you to the wrong place in the program?
If the nesting does exceed 4 levels, the 5th RETURN will take the
program back to the top of the program, not to where you were
expecting. But other than that, and the total limit of 255 GOSUBS,
(which you are unlikely ever to hit!), the GOSUB mechanism is
completely reliable. If you find it is going back to the wrong place,
start looking for your own programming bug.
PBASIC 2.5 has a new command,
ON index GOSUB command0, command1, .....
that is very efficient in some situations. The subroutine executed
is selected by the ordinal index, and the execution returns to the
command after this one. The command counts as only one GOSUB, no
matter how many are in the list.
PBASIC 2.5 offers other alternatives to structures that depend on
GOTO. You can use DO LOOP structures and nest them, and often that
will be more transparent than GOTO label. There is also CASE SELECT.
Those are structures you could implement with a bunch of IFs and
GOTOs, but they are much cleaner and easier to document if you use
the new PBASIC 2.5.
While GOSUB is often the best choice when it comes to repeated
actions and to program clarity, it does have overhead in terms of
both program speed and program size. For example,
GOSUB turnLEDon
.....
turnLEDon:
high 0
RETURN
is very inefficient compared to simply
high LED
whereever you need it.
In general, if you have a program module, it will run tighter and
faster if it does not have a lot of GOSUBs, but if you do elect to
use GOTOs, then it is important to keep the structure clean and tight
anyway.
-- Tracy
--- In basicstamps@yahoogroups.com, Bruce Bates <bvbates@u...> wrote:
> At 02:59 PM 4/16/04 +0000, Allan Lane wrote:
> >Right, right, My Bad -- we were all talking about
> >GOSUB, and someone said all PBASIC running Stamps
> >had a GOSUB level of 4, except those that had
> >a level of 255, and *I* then wanted to say
> >"what are THOSE that have 255?".
> >
> >I should have asked: What Basic Stamp based
> >package has a call depth of 255?
> >
> >I know the Javelin. I own a Javelin. I even
> >LIKE the Javelin. Javelin doesn't HAVE a GOSUB.
>
> Alan -
>
> That "someone" was me. Here is a direct repeat of what I said, just
so there's no misunderstanding. There are TWO separate and distinct
elements to consider:
>
> quote
>
> Also make sure, if there is a limitation, that you have not
exceeded the maximum number of GOSUB statements in a program. For all
PBASIC Stamps, there is a maximum stack depth (GOSUB within GOSUB) of
4 levels. For the BS-1 there are a maximum of 16 GOSUBs permitted per
program. For all other Stamps, this maximum number of GOSUBs is 255.
>
> end quote
>
> The first consideration is the DEPTH of stacking. The second
consideration is the absolute number of GOSUBs within the programs.
The latter differs between the BS-1 and other PBASIC Stamps (BS-2, BS-
2SX, BS-2e, BS-2p), with that difference noted above.
>
> Regards,
>
> Bruce Bates
<allan.lane@h...> wrote:
> I thought ALL stamps ran PBASIC -- what
> other Stamps are there?
They do.
The language in the example with GOTOs was not PBASIC.
It was Yaskawa and Galil Motion Control Programming
on industrial robots.
-Mike
bvbates@u... writes:
This can not happen inadvertently. In other words, so long as there are
logically the same number of GOSUBS and RETURNS, you will always return to the
place from which you left. HOWEVER, if there are NOT an equal number of GOSUB
and
RETURN statements, from a program logic point of view, or if the RETURN stack
is altered, then a spurious RETURN is possible. One of the other things that
you must ensure is that you MUST NOT drop into a RETURN statement which has no
preceding GOSUB. Beware of places where there is a RETURN statement with a
label just preceding it. That is danger in the making!
A few things to remember when using GOSUB and RETURN. Always count to ensure
that you have not exceeded the maximum number of stacked (GOSUB within GOUSUB)
routines as specified by the compiler or interpreter being used. Also make
sure, if there is a limitation, that you have not exceeded the maximum number of
GOSUB statements in a program. For all PBASIC Stamps, there is a maximum
stack depth (GOSUB within GOSUB) of 4 levels. For the BS-1 there are a maximum
of
16 GOSUBs permitted per program. For all other Stamps, this maximum number of
GOSUBs is 255.
I hope that is helpful, and not confusing.
Thanks very much. This was my problem. You guys are great
Tim
[noparse][[/noparse]Non-text portions of this message have been removed]