Goto vs Gosub

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Fred
Newbie
Posts: 18
Joined: Sat Aug 05, 2017 4:44 pm

Goto vs Gosub

Post by Fred » Fri Apr 06, 2018 10:17 pm

Explain me in simple terms, what are the difference between these?
Never used Gosub, always only Goto. Maybe I lose some abilities not using Gosub?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Goto vs Gosub

Post by Marcus Tettmar » Sat Apr 07, 2018 8:54 pm

Goto branches to a label, changing the execution of the script to that line.

Gosub runs a subroutine, returning to the same point when the subroutine has finished.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Fred
Newbie
Posts: 18
Joined: Sat Aug 05, 2017 4:44 pm

Re: Goto vs Gosub

Post by Fred » Sat Apr 07, 2018 9:51 pm

It looks I can do all with Goto>Label without Gosub. When Gosub is preferred to Goto?

Just never even tried to use Gosub, always used Goto>Label, and it always worked. Maybe with Gosub I can make my program shorter or easier to remember if I am back at its code after a year or so?
Can something be done with Gosub, what cant be done with Goto?

psiquetp
Junior Coder
Posts: 25
Joined: Tue Jul 17, 2018 7:56 pm

Re: Goto vs Gosub

Post by psiquetp » Tue Jul 17, 2018 11:03 pm

Gosub should be prefered, there is a reason for most programming languages to have ditched goto statements. That reason has to be the fragility, your code after using goto statements is more prone to error than it is after using gosub.

There are different posts around... here is a more detailed explanation.
https://www.quora.com/Why-are-goto-brea ... er-science

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts