Jump to content

passing data to functions


Recommended Posts

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

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! :party:

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...