tags:

views:

79

answers:

5

Ok I am coming into a stumbling block no matter what language I am using. I am trying to understand when I need to pass arguments in a Function and when I don't need to pass arguments in a function. Can someone give me some direction on where to find guidance on this?

A: 

Well...if a function takes parameters, then you have to pass arguments to it. If it takes no parameters, then you don't. (If you happen to be working in a language in which functions have optional parameters, you only have to pass an argument if you want something other than the default value.)

mipadi
That makes sense.
Moja Ra
+1  A: 

Does the function need external data to perform its job? If so, then you need to pass arguments.

If the function doesn't need external data to perform its job, you don't need to worry about passing arguments.

That handles creating your own functions. If you're simply trying to call somebody else's function, you need to pass arguments for each required function parameter.

Justin Niessner
So I understand that if I am calling a global function from php like the mail() in the parenthesis I put the email address or whatever other value are required which are the same as arguments right?
Moja Ra
Correct, so it would look like `mail('[email protected]','Some Subject', $message);`
Justin Niessner
+1  A: 

I would rather say if your function needs data, you MUST pass parameters, cuz the other alternative is to put the data in a global store and let the function access it from there. DO NOT DO IT as it will make your code nearly impossible to maintain as it grows more complex.

Midhat
Wow see that is a simple answer to a simple question but that is really the only thing I needed to know to help me get down C# and PHP because I never understood what () was for. So now I understand.
Moja Ra
A: 

Well that pretty much depends on what are you trying to accomplish. If your functions needs some values to modify or use you will probably need to pass arguments. Why don't you try it with some examples in some books. Most of them are pretty relevant.

You should not think on what you "need" to pass to a function, you should try to think what are you writing that function for and then you will see if you need arguments or not.

vladv
A: 

Are you talking about existing function or writing your own?

If it is an existing one - you have no choice - in order for it to work you need to pass it whatever it wants. To figure out what it wants - read the manual, the function code, or harass the author of the function

If you are talking about designing your own - it is a much bigger discussion which goes way beyond a single function. You need to understand what the function (and any other components) have to do to accomplish the ultimate goal, how they interact with each other, etc.

mfeingold