AutoHotkey Community

It is currently May 26th, 2012, 4:14 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: April 13th, 2009, 9:25 pm 
Offline

Joined: September 26th, 2008, 6:09 pm
Posts: 82
Location: France
1) In the help I have found :
"Although a function cannot contain definitions of other functions, it can contain subroutines. As with other subroutines, use Gosub to launch them and Return to return (in which case the Return would belong to the Gosub and not the function)."
Does that means that an user function cannot call an other user function ? Or only that the definition of each user function should be separated from the others ? How ?
When several functions use partly common coding (e.g. error routines) it is useful to put the common part in an other function.

2) Can a function have several return statements ? e.g.
if first_condition
return 1
if second_condition
return 2
and so on ?
Or do I have to define a rc variable inside the { } of each condition and an unique "return rc" at the end of the function (that means more {} and indents) ?

3) Where do I put my functions ?
-if they are common to several scripts, #Include of a file that contains all of them, seems very useful.
-at the top of my script ?
-at the bottom of my script below an exit and some comments?

4) Where do I put my subroutines ? Same subquestions as 3) .

5) error messages
Assume that at line nn of "myscript", I call a function "myfunction" and that an error occurs at line mm in the function. And that I wish to put all these informations in my error message.
A_ScriptName should give "myscript", A_ThisFunc = "myfunction", A_LineNumber contains probably mm (pointing to the corresponding ListLines). Where is nn ? The script knows where to return !
Same question for a subroutine.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 9:48 pm 
Offline

Joined: May 28th, 2008, 2:11 am
Posts: 739
Location: Minnesota, USA
1) I believe this example may help
Code:
func1() {
   a = 1
   GoSub, add
   MsgBox % a

   add:
      a++
   Return
}

func2() {
   a = 1
   GoSub, add ; Fails because 'add' is in a different function
   MsgBox % a
}

func1()
func2()


2) Yes it may have multiple return statements
Code:
func1(ret) {
   If (ret = 1)
      Return "Hello "
   If (ret = 2)
      Return "World!"
}

MsgBox % func1(1) . func1(2)


3) It's a matter of preference. They can go at the beginning because they don't interfere with the auto-execute section, but they can also be placed at the end. Usually when I #Include functions I place them after my auto-execute.
Code:
func1() { ; This is fine
   Return 1
}

MsgBox Hello!
Return

func2() { ; This is also fine
   Return 2
}


4) These should probably be placed after the auto-execute section, as they will interfere with it.
Code:
Sub1: ; This is bad - it executes at script start
   MsgBox 1
Return

MsgBox Hello!
Return

Sub2: ; This is good - doesn't execute until called
   MsgBox 2
Return


5)"MyScript.ahk"
Code:
1. myfunction()
2. Return
3.
4. myfunction() {
5.    MsgBox % A_ScriptName ; -> MyScript.ahk
6.    MsgBox % A_ThisFunc   ; -> myfunction
7.    MsgBox % A_LineNumber ; -> 7
8. }

_________________
Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2009, 11:16 pm 
Offline

Joined: March 16th, 2008, 6:48 pm
Posts: 161
Location: Brooklyn
A function can call as many other functions or subroutines as you like. However a function cannot declare a new function.

I like to put all of the functions at the very beginning of the script. Though I think that AHK loads the entire script before a hotkey is run so it's safe to put them anywhere.

If you're going to declare a variable within a function, remember to insert the "global" label.,

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 14th, 2009, 1:33 pm 
Offline

Joined: September 26th, 2008, 6:09 pm
Posts: 82
Location: France
For Slanter
I assume that 1) will work if subroutine add is defined out of the functions.
5)"MyScript.ahk" works as defined in the comments after suppressing line numbers but it does not give the information that myfunction() was called at line 1.
How to obtain the calling line inside the function ? I may define a variable "from" in the line before the call
from := A_LineNumber + 1
and make it global. I was hoping that I have missed the A_... variable keeping this : AHK perfectly knows that the function was called at line 1 and that it has to go to line 2 at the end of the function...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 14th, 2009, 9:41 pm 
Offline

Joined: May 28th, 2008, 2:11 am
Posts: 739
Location: Minnesota, USA
For 1 you are correct. Subroutines declared outside of functions may be called from anywhere in the script. For 5 I don't believe that A_ variable currently exists, your workaround or a similar one is the only way I know of accomplishing this.

_________________
Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Cerberus, poserpro, Yahoo [Bot] and 16 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