Reputation: 872
I may sound stupid to some but I want to know if there is some benefit of passing arguments to a function as an array,rather than passing each arguments or some downsides?
Upvotes: 0
Views: 169
Reputation: 3982
I think that when you have a few parameters or less than 5 (subjectively) then more useful is passing arguments as parameters. If you have a big count of arguments then using array is more useful that function/method with 15 parameters.
Upvotes: 0
Reputation: 7406
Sure it might look nicer if you pass an array to a method, but what does it mean?
Arrays usually signify that you have a collection of the same thing, whilst method parameters are usually different things.
If you want to pass a list of things to a method and do the same action on all of them, then it makes perfect sense to use some type of array/collection object.
If however you want to make it tidier and avoid passing around lots of objects together, consider refactoring your code to use some kind of wrapper object that you can pass around more easily.
Also if you have so many arguments that you would consider using an array to hold them, it's a sure sign that you need to refactor your code ;-)
Upvotes: 2
Reputation: 157886
if you have this array already, it is surely better to use it.
if you don't have this array already, using parameters will save you typing of array
keyword and a couple of braces.
that's all.
Use whatever you feel more suitable for the case.
Upvotes: 0
Reputation: 5039
Parameter list much more clear when you read function signature. Array is just one variable, say, $args. But what there in args?
Upvotes: 0
Reputation: 236
Unwritten rule for good programming practice is that function should not have more than 3-5 arguments.
Usually arrays or even objects are used to pass in the logical complete data structures.
I think this is due more transparent and readable code rather than any performance benefits.
Upvotes: 4
Reputation: 526613
Mostly it just requires more typing to create an array and pass it, rather than just passing individual arguments.
Other than that there's no particular specific advantage to separate parameters.
Upvotes: 0