AutoHotkey Community

It is currently May 27th, 2012, 12:59 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Functions & subroutines
PostPosted: December 17th, 2009, 5:08 pm 
Offline

Joined: March 16th, 2008, 6:48 pm
Posts: 161
Location: Brooklyn
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

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2009, 5:19 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Dynamically Calling a Function

If you have 4 subroutines inside a function, I would consider making them separate functions.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2009, 5:23 pm 
Offline

Joined: April 15th, 2009, 12:05 am
Posts: 75
Location: Italy
yeah
Code:
F11::
copyStackFunction(clearStack)
return

copyStackFunction(clearStack) {
   MsgBox %A_ThisFunc%
   Return   
}

_________________
Intel Centrino @ 2.8GHz
4 GB RAM
WIN XP SP3


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 31st, 2009, 12:59 am 
Offline

Joined: March 16th, 2008, 6:48 pm
Posts: 161
Location: Brooklyn
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

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 31st, 2009, 8:39 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 31st, 2009, 5:38 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 31st, 2009, 5:48 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 31st, 2009, 8:40 pm 
Offline

Joined: March 16th, 2008, 6:48 pm
Posts: 161
Location: Brooklyn
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.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2010, 2:14 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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
}


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher, Leef_me, WillTroll, XstatyK, Yahoo [Bot] and 32 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