-1

If I have a function which calculates the minimum and maximum of an array would it be bad practice to include minimum and maximum in the function parameters so I can edit them while the function runs.

So I would have void minmax(array, int min, int max)

the min and max variables would be sent in as null and get changed when the function runs. Would this be ok or should I be using malloc to get an array and return the pointer instead?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Qwertie
  • 5,784
  • 12
  • 45
  • 89

3 Answers3

3

Q: If I have a function which calculates the minimum and maximum of an array would it be bad practice to include minimum and maximum in the function paramiters

A: No, not at all. That's a perfectly reasonable thing to do.

Q: "So I would have void minmax(array, int min, int max)..."

A: Uh, no. That would NOT pass "min" and "max" back to the caller. Instead, you need something like:

void minman(int * array, int * min, int * max);'

For example:

int myarray[] = {1, 2, 3};
int min, max;
minman(myarray, &min, &max);
...
paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

That's totally fine, except that you'll need min and max to be pointers. If they're just plain old int like you have there, the caller will never see the changes you make.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

There is nothing wrong with a function returning values through parameters. Except the code you wrote will not work - change it like this:

void minmax(int *array, int *min, int *max)

Also see returning multiple values from a function.

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45