AutoHotkey Community

It is currently May 27th, 2012, 10:25 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Subroutine Bug?
PostPosted: August 22nd, 2007, 7:58 pm 
Offline

Joined: May 23rd, 2007, 3:31 pm
Posts: 17
Back 20 years into the last century when I learned to program Subroutines lay dormant unless called. Here is does not. I set up a subroutine:

Code:
subtest:
{
msgbox, this subroutine runs without being called???
return
}


and if it is the first thing in the code sequence it runs without being called, however if I put it behind a hotkey code like this:

Code:
^q::
ExitApp

subtest:
{
msgbox, this subroutine runs without being called???
return
}


it doesn't run. I did stumble across this workaround as I wrote this but it seems to me that subroutines should lie dormant just as hotkeys do, until called.

TIA

Jet


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 22nd, 2007, 8:13 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 969
Location: Frisia
AutoHotkey scripts are structured as follows:

- AutoExecute section (runs until it encounters a Return)
- Subroutines and functions

Since your scripts first Return is the Return after the subroutine (btw: no braces needed), the subroutine will be executed.

Example

Code:
   MsgBox Hello from the autoexecute section

   Gosub, MySubRoutine ; calls the subroutine

   MyFunction() ; calls the function

Return ; End of AutoExecute section

; -------------------------------------------------------

MySubRoutine:
   MsgBox Hello from the subroutine!
Return ; End of subroutine

MyFunction()
{
   MsgBox Hello from the function!
}
; Function does not need a Return


Hope this helps

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Last edited by daonlyfreez on August 22nd, 2007, 9:25 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 22nd, 2007, 8:20 pm 
Offline

Joined: May 23rd, 2007, 3:31 pm
Posts: 17
Thanks for the quick reply.

So just place a return after the stuff I want to autoexecute and all is well?

will I have to remove it should I place something with a return in front of it or will the old return just get ignored?

Thanks,


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 22nd, 2007, 8:57 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
extra returns are ignored. If this does not answer your question, please post again with an example.

Should a moderator move this to Ask for Help?

It is not really a bug.

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 22nd, 2007, 9:23 pm 
Offline

Joined: May 23rd, 2007, 3:31 pm
Posts: 17
Probably so,

Thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 24th, 2007, 12:44 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2545
The reason is that subroutines in AutoHotkey are not like functions without a return value. They are more like bookmarks for goto/gosub type commands/events. Unlike functions, code execution will fall into subroutines and continue.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: chaosad, robotkoer and 71 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group