Sign in to follow this  
Followers 0
h711

What's the best way to pass value from one function to another?

12 posts in this topic

I usually need to pass a lot value from one function in file A to another function in file B. I wonder what is the best way to do that.

Currently I know 3 ways:

1. Use the return value of functions. But this only limit to 1 return.

2. Use byref parameters. Dim them at top model. But this sometime requries a lot parameters.

3. Use Global declaration. If I

global $aaa
in one function, does this make $aaa the same variable in all functions?

Please give some suggestions.

Share this post


Link to post
Share on other sites



I usually need to pass a lot value from one function in file A to another function in file B. I wonder what is the best way to do that.

Currently I know 3 ways:

1. Use the return value of functions. But this only limit to 1 return.

2. Use byref parameters. Dim them at top model. But this sometime requries a lot parameters.

3. Use Global declaration. If I

global $aaa
in one function, does this make $aaa the same variable in all functions?

Please give some suggestions.

Global variables or a global array make the most sense, but the variable name should be declared Global near the top of the script or include file, not inside a function.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

If I declare them in the top module, do I still have to declare them in each of the included files? I got lots problems after I changed some declaration to globle.

Share this post


Link to post
Share on other sites

I think globle variable need to be declared before all the #includes, or you will have all the "WARNING: $: possibly used before declaration." or even error messages.

Share this post


Link to post
Share on other sites

If I declare them in the top module, do I still have to declare them in each of the included files? I got lots problems after I changed some declaration to globle.

No, a global variable should only be declared once.

I think globle variable need to be declared before all the #includes, or you will have all the "WARNING: $: possibly used before declaration." or even error messages.

The normal layout for an AutoIt script (not strictly enforced, but most logical) is to have ALL the #include statements at the very top, then Global variable declarations, then the script code body, and all the locally declared functions at the bottom. Inside those locally declared functions, all variables should either already be declared Global before the function, or should be declared Local inside the function.

Now, some of the Global variables may be declared inside an #include file. If so, they should still only be declared once -- if declared in an #include, the same Global should not be declared in the script. Also, the same Global variable should not be declared in multiple #include files.

If you have some Global variables to declare that are used in multiple #include files, they should be put in their own #include file, then referenced by nesting that #include inside those. To prevent multiple copies of the same #include in the final parsed code, use the #include-once keyword at the top of all #include files.

Examine some of the include files that come with AutoIt (i.e. WinAPI.au3) to see how this has been done already.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I think globle variable need to be declared before all the #includes, or you will have all the "WARNING: $: possibly used before declaration." or even error messages.

Never had this problem, but then my includes are usually pure function-libs, the don't have any main code.

I raised the issue of variable-passing or inheritance when V3 first was launched.. and got flamed for my trouble. (not that being flamed is all that uncommon in here, it seems..) In principle most languages include one of:

A shared variable block between functions.

A father-son inheritance of variables between functions.

A compound variable type which encapsualtes all of the info the function needs in one unit.

In more complex programs, and in the absence of one of these methods, your options are to either use global variables or else to get involved with passing a long list of parameters to each function.

Global variables are frowned-upon by the purists, and in truth they do create issues in that a function which depends on them can never be a true library-routine.

But, having numerous parameters creates a far worse problem, in that adding just one parameter to a toplevel function means updating not only all of the calls to this function throughout the program, but all to nested functions as well, if these reference the variables of the parent. This can end-up with a 'snowball effect' in which adding just one parameter requires almost a total app rewrite.

In principle I try to avoid having more than 3-6 parameters on a function. If more are needed, I try to look for an alternative approach. Which might include globals. :)

Perhaps I'm stretching the limits of what is basically a scripting language, as these issues don't typically affect programs of a few dozen, or at most a few hundred lines, which are its intended purpose... or am I?

Share this post


Link to post
Share on other sites

Perhaps I'm stretching the limits of what is basically a scripting language, as these issues don't typically affect programs of a few dozen, or at most a few hundred lines, which are its intended purpose... or am I?

You make a good point there. AutoIt is an interpreted scripting language optimized for a small, tight interpreter executable.

Wishful thinking about complex inheritance schemes and powerful libraries makes as much sense as wishing for a mobile home that gets 40MPG and includes an indoor lap pool.

And the OP never provided enough information about how much and what kind of values needed to be passed to get specific about best practice within the limitations of AutoIt.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Wishful thinking about complex inheritance schemes and powerful libraries makes as much sense as wishing for a mobile home that gets 40MPG and includes an indoor lap pool.

I'd put the pool on the roof. Takes up less floor space. Plus, it would be shaded from the sun by the radiotelescope.

Share this post


Link to post
Share on other sites

I'd put the pool on the roof. Takes up less floor space. Plus, it would be shaded from the sun by the radiotelescope.

No can do! Two lane bowling alley is already there... guess it just has to have another story added.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I do have some random warming and possible error messages if the globle is declared after include.

Any way, I need some advised on the layout.

Let's say I need to automate an utility and check for errors. I think it could be 1000-2000 lines of code and maybe 10-20 functions. Even this is not a large project in terms of line of code, but it will still be too messy to put every thing in one file. Currently I put the main script for the utility in one file, GUI for the automation in one file, and the script that checks Window information in another file.

This is my first AutoIt project, and there could be many more after this one. So I do want to make everything more modular and standarized. I have some questions:

1. What is the difference between a script and a libary file? Is script only deal with a specific application, but the functions in a libary file can be used for many applications?

2. What is an automation framework? Do we all use the same framework that defined by Autoit?

Thanks for the help!

Share this post


Link to post
Share on other sites

I do have some random warming and possible error messages if the globle is declared after include.

Then the global variable is being used by functions in the #include file. In that case the Global declaration should be in the #include file, not the script.

Any way, I need some advised on the layout.

Let's say I need to automate an utility and check for errors. I think it could be 1000-2000 lines of code and maybe 10-20 functions. Even this is not a large project in terms of line of code, but it will still be too messy to put every thing in one file. Currently I put the main script for the utility in one file, GUI for the automation in one file, and the script that checks Window information in another file.

Don't think of it as breaking up a big script. There's no need to break up a script just because of line count.

Think more of reusable code. Write your functions so they can be reused in multiple scripts, put them in an #include file, and you can include those functions without having to rewrite them. The only Global variables used by code in the #include file should be declared in the #include file (or in another #include reference within it). An include file should never require Globals declared that it doesn't provide for itself.

This is my first AutoIt project, and there could be many more after this one. So I do want to make everything more modular and standarized. I have some questions:

1. What is the difference between a script and a libary file? Is script only deal with a specific application, but the functions in a libary file can be used for many applications?

Proper #include files contain generic functions and declarations that can be reused in multiple scripts. Code that is particular to a specific script belongs in the script itself.

2. What is an automation framework? Do we all use the same framework that defined by Autoit?

I've never heard that term in relation to AutoIt. Where did you pick up "automation framework"? That could mean batch files, WMI, DLLs, VBS, AutoIt, ...?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I've never heard that term in relation to AutoIt. Where did you pick up "automation framework"? That could mean batch files, WMI, DLLs, VBS, AutoIt, ...?

I saw the term "automation framework" in many job AI for automation engineer positions. Also, during an interview, people may ask you "Do you build the framework yourself, or you just scripting?". I think this term is refer to reusable functions that put in a libary file. But some people build GUI atuomation framework by using perl or Python. They build everything themselve from API level. So I guess what I am doing with AutoIt can only be called "scripting" because I use all the existing functions.

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