| View previous topic :: View next topic |
| Author |
Message |
Sergio
Joined: 16 Mar 2008 Posts: 160 Location: Brooklyn
|
Posted: Thu Dec 17, 2009 4:08 pm Post subject: Functions & subroutines |
|
|
Is there any way to call a function & a subroutine dynamically. I have a function called copyStackFunction. It has four subroutines: copyStack, pasteStack, removeStack & clearStack. So the function is created and the subroutines are set. Can't I just do something similar to this?
| Quote: | F1::
copyStackFunction(clearStack)
return |
_________________
 |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Thu Dec 17, 2009 4:19 pm Post subject: |
|
|
Dynamically Calling a Function
If you have 4 subroutines inside a function, I would consider making them separate functions. |
|
| Back to top |
|
 |
UncleScrooge
Joined: 14 Apr 2009 Posts: 75 Location: Italy
|
Posted: Thu Dec 17, 2009 4:23 pm Post subject: |
|
|
yeah
| Code: | F11::
copyStackFunction(clearStack)
return
copyStackFunction(clearStack) {
MsgBox %A_ThisFunc%
Return
}
|
_________________ Intel Centrino @ 2.8GHz
4 GB RAM
WIN XP SP3 |
|
| Back to top |
|
 |
Sergio
Joined: 16 Mar 2008 Posts: 160 Location: Brooklyn
|
Posted: Wed Dec 30, 2009 11:59 pm Post subject: |
|
|
I was actually looking for something more along these lines. I need it to be a single function (so that it can be called from a library). Thanks for the help though.
| Code: | mathFunction(operator, aVar, bVar)
{
Gosub, %operator%
return
add:
answer:= aVar + bVar
msgbox, %answer%
return
multiply:
answer:= aVar * bVar
msgbox, %answer%
return
}
^1::
mathFunction("multiply",4,5)
return |
_________________
 |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Thu Dec 31, 2009 7:39 am Post subject: |
|
|
You don't need gosubs in your function: | Code: | #z::
mathFunction("multiply",4,5)
Return
mathFunction(operator, aVar, bVar) {
If (Operator = "Add")
{
answer:= aVar + bVar
msgbox, %answer%
Return
}
If (Operator = "Multiply")
{
answer:= aVar * bVar
msgbox, %answer%
Return
}
} |
_________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Thu Dec 31, 2009 4:38 pm Post subject: |
|
|
| Sergio wrote: | | I need it to be a single function (so that it can be called from a library). | Your reasoning seems flawed in that function library scripts can have any number of functions. Perhaps I am misunderstanding?
| Code: | mathFunction(operator, aVar, bVar) {
msgbox % mathFunction_%operator%(aVar, bVar)
}
mathFunction_add(aVar, bVar) {
return aVar + bVar
}
mathFunction_multiply(aVar, bVar) {
return aVar * bVar
}
^1::mathFunction("multiply",4,5)
| If a script explicitly calls mathFunction_add() and it has not already been defined, AutoHotkey will attempt to #include mathFunction_add.ahk, and when that fails, mathFunction.ahk (based on the placement of the first underscore). However, if these sub-functions are only ever called by mathFunction itself, it really doesn't matter what they're named.
| hugov wrote: | | You don't need gosubs in your function: | You don't need Ifs if you have Gosubs. Do you have a reason for suggesting one method over the other? |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Thu Dec 31, 2009 4:48 pm Post subject: |
|
|
| Lexikos wrote: | | hugov wrote: | | You don't need gosubs in your function: | You don't need Ifs if you have Gosubs. Do you have a reason for suggesting one method over the other? | No not really, was just easier (for me that is) to incorporate in the already posted code. Not all my answers are correct or make sense. _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Sergio
Joined: 16 Mar 2008 Posts: 160 Location: Brooklyn
|
Posted: Thu Dec 31, 2009 7:40 pm Post subject: |
|
|
| Lexikos wrote: | | Your reasoning seems flawed in that function library scripts can have any number of functions. Perhaps I am misunderstanding? |
I wanted to keep it all in one .ahk file for reasons that are not really pertinent now. I could do it one way or the other I suppose. My only concern is that the real purpose of the function is to copy variables and they need to be transferred between one another. I'm not sure if that would be made more difficult.
| hugov wrote: | | No not really, was just easier (for me that is) to incorporate in the already posted code. Not all my answers are correct or make sense. |
Well, it's not better or worse, but it does provide a different set of options. That's always good. _________________
 |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Fri Jan 01, 2010 1:14 am Post subject: |
|
|
| Sergio wrote: | | I wanted to keep it all in one .ahk file for reasons that are not really pertinent now. | Is that a problem? As I said, if a script calls mathFunction_add() and mathFunction_add.ahk does not exist, mathFunction.ahk will be #included.
| Quote: | | My only concern is that the real purpose of the function is to copy variables and they need to be transferred between one another. I'm not sure if that would be made more difficult. | I don't see why it would, but I'm not sure what you're doing. I suppose the main issues would be if you needed to share static variables or arrays of local variables. In that case you may need to use subroutines within a single function. Goto might improve readability by allowing the value to be returned directly:
| Code: | mathFunction(operator, aVar, bVar) {
answer:= mathFunction_(operator, aVar, bVar)
msgbox, %answer%
return answer
}
mathFunction_(operator, aVar, bVar) {
; Variables can be shared by subroutines within this function.
Goto %operator%
add:
return aVar + bVar
multiply:
return aVar * bVar
}
|
|
|
| Back to top |
|
 |
|