At the time of this writing, here is what your code looks like :
int max (int a, int b, int c, int d){
int result = a;
result > b ? result : result = b;
result > c ? result : result = c;
result > d ? result : result = d;
return result;
}
Those ternary expressions still don't look right. They should be of the form:
result = ( result > b ) ? result : b;
Also, naming a c++ function
max
is not a good idea. There is a macro named
max
in the standard C headers and the Standard Template Library has a template function named
max
. It would better to use a different name for this function. I prefer to use a function that assigns the result to a value that is passed in. Here's an example :
__inline void AssignMax( int & maxvalue, int newvalue )
{
if( maxvalue < newvalue )
maxvalue = newvalue;
}
and then you can rewrite your function like this :
int GetMaxValue( int a, int b, int c, int d )
{
result = a;
AssignMax( result, b );
AssignMax( result, c );
AssignMax( result, d );
return result;
};
One advantage to this approach is it does not constantly assign the value to result. It only does so when the argument passed is larger than it.