AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Difference between GoSub and functions?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
helpme



Joined: 22 Apr 2007
Posts: 33

PostPosted: Wed Jun 06, 2007 3:26 am    Post subject: Difference between GoSub and functions? Reply with quote

Dear AutoHotkey Gurus,

What is the difference between GoSub and Functions? In what kind of situation is it better to use GoSub as compared to Functions?

Thank you for your guidance.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Wed Jun 06, 2007 3:33 am    Post subject: Reply with quote

they are two different methods of programming. Functions are slightly more complicated, but much easier to reuse. They are also capable of returning a value. Gosubs make code (slightly) harder to follow, but it really is about your preferences. Guis, for example use Gosub by default for buttons, so you need to provide a label for the button to activate.

They both have their uses, but just get used to whichever one you like. Most of things you find in the forum will be functions.

In my code, I tend to use functions for just one step of a procedure, then use gosubs for everything else.
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
ManaUser



Joined: 24 May 2007
Posts: 1121

PostPosted: Wed Jun 06, 2007 3:41 am    Post subject: Reply with quote

I guess I probably don't qualify is an AutoHotkey Guru yet but...

The biggest difference is that a function can return a value, so you can do something like this:
MyVar := MyFunction()

Another important differnce is that functions, by default, have their own set of variables. That's handy in a really complicated script where you would risk accidently naming two variables in different parts the same thing. Or if you're using a fuction written by someone else.

So basically you could really use functions all the time. In fact that's the standard way to to do things in some languages. If I were and AutoHotkey Guru I could tell you if functions have any extra memory or procesor overhead asociated with them but I'm not. My guess is that they do, but not too much.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Wed Jun 06, 2007 3:46 am    Post subject: Reply with quote

I'm not a guru, just a helper, but I think there is not a significant speed or memory difference. There are a few benchmarking scripts you could use to test.
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
helpme



Joined: 22 Apr 2007
Posts: 33

PostPosted: Wed Jun 06, 2007 9:14 am    Post subject: Reply with quote

Thank you Gurus.

I have a simple mind and get confused easily, so I better stick to functions as they will make my code more readable and less messy.

Thanks!
Back to top
View user's profile Send private message
svi



Joined: 09 Oct 2006
Posts: 236
Location: Finland

PostPosted: Wed Jun 06, 2007 3:55 pm    Post subject: Reply with quote

1.
Manual wrote:
A function is similar to a subroutine (Gosub) except that it can accept parameters (inputs) from its caller. In addition, a function may optionally return a value to its caller.
(as mentioned)

2. Local vs. global variables.
(as mentioned)

3. A funcion definitinion can be anywhere in the script, also at the beginning. It won't be executed before a function call.
(as NOT mentioned Smile )
_________________
Pekka Vartto
Back to top
View user's profile Send private message
POINTS



Joined: 17 Jan 2006
Posts: 290

PostPosted: Wed Jun 06, 2007 7:49 pm    Post subject: Functions Reply with quote

Functions are preferable because it makes your code easier to read and understand. You should use functions WHENEVER POSSIBLE (other than Gui labels of course). However, there are some times when GoSub makes more sense, but whenever you can try to use functions.
_________________
My AutoHotkey Program for Warcraft III:
Warkeys
http://warkeys.sourceforge.net/

Remap your hotkeys
Healthbars always on
Remap inventory
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group