Your function's prototype should not be inside another function, as your are doing here. It should also be BEFORE the function definition.
Change the function's return type from void to int . The return the value of 'max'. All that's necessary is for you to call the function in the cout call, and the value will be printed out.
By the way, how come you declare the variable 'other' when you don't have any use for it?
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
you should just output the maximum of the two:
cout << "the max is " << max(firstnum, secondnum)) << endl;
that said, your max function is wrong. It finds the max but should return the result. Here's a cleaner version:
int max(int a, int b)
{
if(a > b)
return a;
else
return b;
}
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
All that's necessary is for you to call the function in the cout call, and the value will be printed out.
you should just output the maximum of the two:
cout << "the max is " << max(firstnum, secondnum)) << endl;
That's what I meant to say. Really. :o
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Here's a cleaner version:
int max(int a, int b)
{
if(a > b)
return a;
else
return b;
}
Except multiple returns from a single function is frowned upon. Best to keep your original function with thereturn changes mentioned above, and with one other minor change:
if (x >= y)
{ // find the maximum number
max = x;
}
else
{
// (x <= y); remove this line, it does nothing
max = y;
}
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Except multiple returns from a single function is frowned upon.
Hmm...now that is some news ;)
The statement is not absolute as such. It all depends on the problem under consideration.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Except multiple returns from a single function is frowned upon.
That's more a matter of style for ease of maintenance, AFAICT. IMHO, this function is more clear this way, though if you want a single return, it could be done as
int max(int a, int b) {
return (a > b ? a : b);
}
but I didn't want to post that one for clarity's sake.
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
The statement is not absolute as such. It all depends on the problem under consideration.
Which is why I stated it as I did -- not as an absolute. ;)That's more a matter of style for ease of maintenance, AFAICT. IMHO, this function is more clear this way, though if you want a single return, it could be done as
int max(int a, int b) {
return (a > b ? a : b);
}
but I didn't want to post that one for clarity's sake.
Very good choice!!
Purists frown on multiple returns. They can be harder to debug because sometimes a return gets lost in complex or long functions. IMO, you shouldtry to use only one return point if the code to support it does not make the code overly complex. But don't make the code difficult to read to accomplish it.
For this piece of code I'd probably use:
int max(int a, int b)
{
int rtn = a;
if(a < b) rtn = b;
return rtn;
}
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I guess it will depend on the coding standards of the company you are working for. Here are two implementations of the function which finds the index of an element in the array and returns -1 if not found.
// MULTIPLE RETURNS
int indexOf2 (int array[], int length, int element)
{
for (int i = 0; i < length; ++i)
{
if (array [i] == element)
return i ;
}
return -1 ;
}
// SINGLE RETURN
int indexOf1 (int array[], int length, int element)
{
int index = -1 ;
for (int i = 0; i < length; ++i)
{
if (array [i] == element)
{
index = i ;
break ; // btw even break and continue are frowned upon
}
}
return index ;
}
Take your pick... ;)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>Why are these things frowmed upon?
Reread WaltP's post:
Purists frown on multiple returns. They can be harder to debug because sometimes a return gets lost in complex or long functions.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339