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 

Clarification on functions or subroutines

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



Joined: 26 Sep 2008
Posts: 82
Location: France

PostPosted: Mon Apr 13, 2009 8:25 pm    Post subject: Clarification on functions or subroutines Reply with quote

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.
Back to top
View user's profile Send private message
Slanter



Joined: 28 May 2008
Posts: 739
Location: Minnesota, USA

PostPosted: Mon Apr 13, 2009 8:48 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
Sergio



Joined: 16 Mar 2008
Posts: 160
Location: Brooklyn

PostPosted: Mon Apr 13, 2009 10:16 pm    Post subject: Reply with quote

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.,
_________________
Back to top
View user's profile Send private message
nigelle



Joined: 26 Sep 2008
Posts: 82
Location: France

PostPosted: Tue Apr 14, 2009 12:33 pm    Post subject: Reply with quote

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...
Back to top
View user's profile Send private message
Slanter



Joined: 28 May 2008
Posts: 739
Location: Minnesota, USA

PostPosted: Tue Apr 14, 2009 8:41 pm    Post subject: Reply with quote

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