Sign in to follow this  
Followers 0
cag8f

Can function exist in separate file?

8 posts in this topic

Hi all. I'm an amateur coder who is playing around with AutoIt for the first time. 2 questions, the first simple, the second more involved:

1. Is there a way for a function to exist on its own as a separate file? I'd then like to be able to call that function with parameters.

2. I'm trying to do this to test a rather complicated program (complicated for me at least). I'd obviously like to test my program as I keep adding to it. However testing it requires changing a handful of lines of code each time. To efficiently test my program, I thought of changing it to a function. Then I could call the function with a test parameter, which would tell the function to use certain lines of code for testing. Does that make sense? I'm sure people who code for a living have much better testing/QA methods. If anyone has the time, I'd really love to hear them.

Thanks in advance!

Share this post


Link to post
Share on other sites



cag8f,

Two ways to do what you want:

- 1. Use #include to get the "function" file into your main script. The content of the file will be incorporated into your main script at the point where you have the #include line - so make sure all the required variables are declared beforehand. If you do this, passing parameters is as easy as it is for any other function and getting return values is simple. :D

- 2. Keep the file as a separate entity and run it from within your main script. In this case you would have to pass your parameters as part of the invocation command line within the main script:

Run(@ScriptFullPath & " /AutoIt3ExecuteScript Function_file.au3 param1 param2 ...")

You would then use the special $CmdLine array to determine what the parameters are - details are in the Help file under <Using AutoIt - Command Line Parameters>. However, getting any return values back to the main script is more tricky - there are lots of threads on inter-script communication to help, though. :graduated:

Over to you - you know where we are if you run into problems. :(

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thanks for the reply. I forgot to mention that I looked into #include. It seemed to be perfect, but I couldn't figure out how to pass parameters to the file. Is it as simple as:

$param_1 = 1 ;Parameter 1 is defined in the main script

#include <auxscript.au3> ; Does aux script now know the value of parameter 1?

Share this post


Link to post
Share on other sites

Nevermind, tested it and that appears to be how it works. Didn't think to try that very simple method. Thanks!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

cag8f,

I couldn't figure out how to pass parameters to the file

The joy of using #include is that you do not have to! The file in already in your script. :graduated:

A short example. Save this as "file_2.au3":

Func _Sum($i1, $i2)

    Return ($i1 + $i2)

EndFunc

Now save this as "file_1.au3" and run it in SciTE:

$iParam_1 = 1
$iParam_2 = 2

MsgBox(0, "Result", _Sum($iParam_1, $iParam_2))

#include "file_2.au3"

You see that it works passing values each way. :(

The #include directive means that "file_1.au3" actually looks like this as far as AutoIt is concerned:

$iParam_1 = 1
$iParam_2 = 2

MsgBox(0, "Result", _Sum($iParam_1, $iParam_2))

Func _Sum($i1, $i2)

    Return ($i1 + $i2)

EndFunc

So you can see why you have no problems. :D

All clear? :D

M23

Edit: I see it is!

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thanks again for the help.

Share this post


Link to post
Share on other sites

cag8f,

My pleasure. :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0