Echoing others, your
similarValuesfunction should be returning a boolean. Calling itareSimilarValuesreads better and is more descriptive.The two arguments are called
smallerValueandbiggerValue, yet you don't actually know that one is bigger than the other. It's equally as plausible you could call the function like this:similarValues(55, 0.1)in which case the variable names are completely wrong from their actual values. I would recommend something more generic in this case:aandborvalue1andvalue2.The
similarValuesfunction can be reduced to one line:#include <cmath> bool function areSimilarValues(double a, double b) { return abs(a - b) < 0.01; }Also note that no comments are required when you give things descriptive names, and use appropriate return types.
Like others have said, the
valueLargerandvalueSmallerfunctions could use better names, such asgetLargerValueandgetSmallerValue.The
valueLargerandvalueSmallerfunctions return zero when the values are equal? The otherreturnstatements return one of the numbers. Returning zero feels like a logic error. If they are equal, just return one of them. It doesn't really matter which one you return since they are equal.double getLargerValue(double a, double b) { if (a > b) { return a; } else if (b > a) { return b; } else { // Values are equal return a; } }Even this could be reduced to:
double getLargerValue(double a, double b) { return b > a ? b : a; }If
bis greater thana, returnb. Otherwise,ais either larger thanboraandbare equal, so returna. And just for good measure, thegetSmallerValuefunction:double getSmallerValue(double a, double b) { return b < a ? b : a; }Now that I think about it, Mathematics has a term for getting the larger or smaller value of two numbers: Maximum and Minimum. These would be good names for
valueLargerandvalueSmaller:double maximum(double a, double b) { } double minimum(double a, double b) { }