Difference between GoSub & Goto?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Sam71
Junior Coder
Posts: 23
Joined: Tue Mar 12, 2019 10:10 pm

Difference between GoSub & Goto?

Post by Sam71 » Wed Apr 03, 2019 4:41 am

Hi,

I was just wondering what the difference is between the commands "GoSub" and "Goto"?.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1384
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Difference between GoSub & Goto?

Post by Dorian (MJT support) » Wed Apr 03, 2019 12:13 pm

Goto will jump to a label and carry on from there, missing out everything inbetween. So:

Code: Select all

MDL>Hi
Goto>Jump
MDL>This line will be skipped
Label>Jump
MDL>And carry on from here.
Whereas Gosub jumps to a Subroutine - a self contained section of code - executes all the code in that subroutine, then goes back to next line after the gosub. This is useful if you have certain code you use a lot. You don't have to keep repeating it. I know you use Image Recognition a lot, so you could, for example, use it to look for a certain image wherever you used it in your script, without needing to use the same Image Recognition code over and over. Like this :

Code: Select all

Gosub>LookforImage1
..do something else
Gosub>LookforImage2
..do something else
Gosub>LookforImage3
..do something else
//look for image 2 again
Gosub>LookforImage2

SRT>LookforImage1
  FindImagePos>%BMP_DIR%\image_1.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  LClick
Endif
END>LookforImage1

SRT>LookforImage2
  FindImagePos>%BMP_DIR%\image_2.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  LClick
Endif
END>LookforImage2

SRT>LookforImage3
  FindImagePos>%BMP_DIR%\image_3.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  LClick
Endif
END>LookforImage3
Try experimenting with this very simple example in the editor, with >Debug >Trace so you can see it run line-by-line

Code: Select all

Gosub>MySubroutine
MDL>Now I am back up here
Gosub>MySubroutine

SRT>MySubroutine
  MDL>I jumped down here
END>MySubroutine
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Sam71
Junior Coder
Posts: 23
Joined: Tue Mar 12, 2019 10:10 pm

Re: Difference between GoSub & Goto?

Post by Sam71 » Fri Apr 05, 2019 6:33 am

Dorian (MJT support) wrote:
Wed Apr 03, 2019 12:13 pm
Goto will jump to a label and carry on from there, missing out everything inbetween. So:

Code: Select all

MDL>Hi
Goto>Jump
MDL>This line will be skipped
Label>Jump
MDL>And carry on from here.
Whereas Gosub jumps to a Subroutine - a self contained section of code - executes all the code in that subroutine, then goes back to next line after the gosub. This is useful if you have certain code you use a lot. You don't have to keep repeating it. I know you use Image Recognition a lot, so you could, for example, use it to look for a certain image wherever you used it in your script, without needing to use the same Image Recognition code over and over. Like this :

Code: Select all

Gosub>LookforImage1
..do something else
Gosub>LookforImage2
..do something else
Gosub>LookforImage3
..do something else
//look for image 2 again
Gosub>LookforImage2

SRT>LookforImage1
  FindImagePos>%BMP_DIR%\image_1.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  LClick
Endif
END>LookforImage1

SRT>LookforImage2
  FindImagePos>%BMP_DIR%\image_2.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  LClick
Endif
END>LookforImage2

SRT>LookforImage3
  FindImagePos>%BMP_DIR%\image_3.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  LClick
Endif
END>LookforImage3
Try experimenting with this very simple example in the editor, with >Debug >Trace so you can see it run line-by-line

Code: Select all

Gosub>MySubroutine
MDL>Now I am back up here
Gosub>MySubroutine

SRT>MySubroutine
  MDL>I jumped down here
END>MySubroutine
Hi Dorian,

Am I right in thinking that the gosub runs faster than say the other types of loops like goto etc?. I'll be testing this out ASAP.
Thanks,
Sam

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1384
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Difference between GoSub & Goto?

Post by Dorian (MJT support) » Fri Apr 05, 2019 1:21 pm

Tbh I'm not sure there'd be any difference, speed-wise. In my opinion they are more convenient if you're using the same routines over and over.
Yes, we have a Custom Scripting Service. Message me or go here

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