| View previous topic :: View next topic |
| Author |
Message |
helpme
Joined: 22 Apr 2007 Posts: 33
|
Posted: Wed Jun 06, 2007 3:26 am Post subject: Difference between GoSub and functions? |
|
|
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 |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 8255 Location: Maywood, IL
|
Posted: Wed Jun 06, 2007 3:33 am Post subject: |
|
|
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 |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 1121
|
Posted: Wed Jun 06, 2007 3:41 am Post subject: |
|
|
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 |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 8255 Location: Maywood, IL
|
Posted: Wed Jun 06, 2007 3:46 am Post subject: |
|
|
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 |
|
 |
helpme
Joined: 22 Apr 2007 Posts: 33
|
Posted: Wed Jun 06, 2007 9:14 am Post subject: |
|
|
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 |
|
 |
svi
Joined: 09 Oct 2006 Posts: 236 Location: Finland
|
Posted: Wed Jun 06, 2007 3:55 pm Post subject: |
|
|
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 ) _________________ Pekka Vartto |
|
| Back to top |
|
 |
POINTS
Joined: 17 Jan 2006 Posts: 290
|
Posted: Wed Jun 06, 2007 7:49 pm Post subject: Functions |
|
|
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 |
|
 |
|