frew Posted May 20, 2009 Share Posted May 20, 2009 Hello, I was just hoping to get any ideas, tips, links, etc. related to the general practice of passing data to functions. I'm just starting to get a little understanding of what's going on, but it all still feels a bit unclear. I guess the basic idea is that functions do things, and we can give the function specific data that we want the function to do something with. So I'm just asking for any general concepts, ways of thinking about what passing data to a function is actually doing, or points to keep in mind, general good practices, etc. as I try to get a practical understanding of "passing variables to functions" etc. Here's a little code with a couple questions in the comments: $Cube1 = 0 _Cube(3);3 is the "argument" that gets passed to the _Cube() function (is that correct?) MsgBox(0, '', $Cube1) Func _Cube($AnyVariabelName);$AnyVariabelName is the "parameter" that holds the argument? $Cube1 = $AnyVariabelName * $AnyVariabelName * $AnyVariabelName EndFunc for example, I am also wondering, why can _Cube($AnyVariabelName) have any name there? Thank you, frew Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 20, 2009 Share Posted May 20, 2009 (edited) Hello, I was just hoping to get any ideas, tips, links, etc. related to the general practice of passing data to functions. I'm just starting to get a little understanding of what's going on, but it all still feels a bit unclear. I guess the basic idea is that functions do things, and we can give the function specific data that we want the function to do something with. So I'm just asking for any general concepts, ways of thinking about what passing data to a function is actually doing, or points to keep in mind, general good practices, etc. as I try to get a practical understanding of "passing variables to functions" etc. Here's a little code with a couple questions in the comments: $Cube1 = 0 _Cube(3);3 is the "argument" that gets passed to the _Cube() function (is that correct?) MsgBox(0, '', $Cube1) Func _Cube($AnyVariabelName);$AnyVariabelName is the "parameter" that holds the argument? $Cube1 = $AnyVariabelName * $AnyVariabelName * $AnyVariabelName EndFunc for example, I am also wondering, why can _Cube($AnyVariabelName) have any name there? Thank you, frew Starting off with specific questions about a specific piece of code gets you +5 Brownie Points! The value passed to _Cube() is the integer 3. Inside that function, the value passed is referred to by whatever name you give it in the function declaration (i.e. $AnyVariabelName [sic]). Input variables in a function declaration are automatically declared LOCAL in that function. By default any other variables declared inside a function are LOCAL, unless a matching GLOBAL already exists. Your example declares $Cube1 outside of any function, so it is declared GLOBAL by default. It is bad practice to let the default behavior take care of variable scope because it leads to confusion. Much better to explicitly declare Global or Local (don't even bother with DIM). Your function return results by modifying a GLOBAL variable, or it could simply RETURN the value: Global $Cube1 = _Cube(3) MsgBox(0, '', $Cube1) Func _Cube($AnyVariabelName) Local $RET = $AnyVariabelName ^ 3 Return $RET EndFunc Edited May 20, 2009 by PsaltyDS 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 Link to comment Share on other sites More sharing options...
Coolw Posted May 20, 2009 Share Posted May 20, 2009 (edited) Beat to it. Edited May 20, 2009 by Coolw My ProgramsMy WIP'sSteam Server Restarter Link to comment Share on other sites More sharing options...
frew Posted May 20, 2009 Author Share Posted May 20, 2009 Wow, just what I needed. Thanks so much PsaltyDS! Now I'm studying your great explanations to finally understand this. Thanks again, frew Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now