Question

Considering two functions:

int foo(int, int);

and

int bar(int*);

(Assuming bar is passed in an array of size n where n = # of foo params, the respective values are equivalent, and the functions are functionally the same)

Is it ever 'better' to take the former approach over the latter?

Was it helpful?

Solution 3

One disadvantage I can think of with int bar(int*) is that it could make overloading more difficult. E.g.:

int areaOfRectangle(int width, int height);
int areaOfRectangle(int x1, int y1, int x2, int y2);

is fine, but how are you going to do that with arrays?

int areaOfRectangle(int* widthHeight);
int areaOfRectangle(int* coordinates); // error

The above won't compile because int areaOfRectangle(int*) can't be declared/defined twice.

Also, if you're simply using an array as a way to arbitrarily group parameters, you make your code harder to read and use. Compare:

int calculateIncome(int normalHours, 
                    int overtimeHours, 
                    int normalRate
                    int overtimeRate); // Self documenting

int calculateIncome(int* parameters); // How do I use this function?

int calculateMean(int* numbers); // Parameters are logically grouped, 
                                 // so array makes sense

A third issue (which Sancho describes in his answer) is that int bar(int*) is dangerous. What happens when the user calls your function a smaller array than the one you were expecting? A call to int Foo(int, int) on the other hand, with the wrong number of parameters, will not compile.

OTHER TIPS

It depends on what you mean by "better": if passing multiple parameters is better aligned with the logical structure of your program, then it is definitely much better for readability to pass multiple parameters. A good test of whether or not this may be the case is asking if individual elements of the array would benefit from being named individually. If the answer is "yes", then individual parameters are better. Saving a few bytes here and there when passing parameters does not compensate for even a slight loss of readability.

Yes, the former approach int foo(int, int); can check the parameters at compile time. int bar(int*) needs to make assumptions about the passed-in array.

It Depends on What the "better" is:

  1. For better performance:

    1 If the array have only a few element( say less than 6 parameter, depends on the target CPU and the available registers can be used to pass parameters) , the foo() method is better, since all variable can be pass by register; and foo can directly get the value out from the register. While for bar, a dereference of the pointer is needed.

    2 IF the array have more than 10 items. It is better to pass using pointers, since this have less stack operations to push the parameter on the top of the stack.

  2. For less bug: the foo() method is better, since for the bar() method, the items can be modified by statements in bar which can be visible after returning from bar(), and this may cause error if you does not maintain the operations of items in bar() carefully.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top