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 

Functions & subroutines

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



Joined: 16 Mar 2008
Posts: 160
Location: Brooklyn

PostPosted: Thu Dec 17, 2009 4:08 pm    Post subject: Functions & subroutines Reply with quote

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
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Thu Dec 17, 2009 4:19 pm    Post subject: Reply with quote

Dynamically Calling a Function

If you have 4 subroutines inside a function, I would consider making them separate functions.
Back to top
View user's profile Send private message Visit poster's website
UncleScrooge



Joined: 14 Apr 2009
Posts: 75
Location: Italy

PostPosted: Thu Dec 17, 2009 4:23 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
Sergio



Joined: 16 Mar 2008
Posts: 160
Location: Brooklyn

PostPosted: Wed Dec 30, 2009 11:59 pm    Post subject: Reply with quote

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
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Thu Dec 31, 2009 7:39 am    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Thu Dec 31, 2009 4:38 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Thu Dec 31, 2009 4:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
Sergio



Joined: 16 Mar 2008
Posts: 160
Location: Brooklyn

PostPosted: Thu Dec 31, 2009 7:40 pm    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Fri Jan 01, 2010 1:14 am    Post subject: Reply with quote

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
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