Re: Find the Lowest of These
Code:
int nLowest = num1;
if(num2 < nLowest)
nLowest = num2;
//repeat the last two lines for the remaining numbers
return nLowest;
Re: Find the Lowest of These
You should either be passing your parameters by value or by const reference. For primitive types like int, value is slightly preferable. Passing by non-const reference when not necessary is bad form.
Re: Find the Lowest of These
Quote:
Originally Posted by
GCDEF
Code:
int nLowest = num1;
if(num2 < nLowest)
nLowest = num2;
//repeat the last two lines for the remaining numbers
return nLowest;
wow, i cant believe i didnt think of that, lol...
thanks man :D
Re: Find the Lowest of These
Quote:
Originally Posted by
Lindley
You should either be passing your parameters by value or by const reference. For primitive types like int, value is slightly preferable. Passing by non-const reference when not necessary is bad form.
1.) This is my first assignment to get me started on function prototypes.
2.) How is this bad form/not necessary?
Re: Find the Lowest of These
Quote:
Originally Posted by
iiSoMeGuY 7x
2.) How is this bad form/not necessary?
Passing by non-const reference means that changes made to those parameters within the function will be reflected in the variables *outside* the function. This is occasionally desirable (output parameters let a function "return" more than one value), but is not usually the intention.
A rule of thumb is: it's always easier to find the bugs that the compiler can catch than those which don't show up until you run the code, so give the compiler all the help you can. If you don't intend a parameter to be modifiable by a function, then don't allow it to be. Make it a const reference. Or, pass by value so that changes made to that parameter within the function don't matter to any larger scope.
Code:
int findLowest(int, int, int, int, int);// pass by value
int findLowest(int &, int &, int &, int &, int &);// pass by reference
int findLowest(const int &, const int &, const int &, const int &, const int &);// pass by const reference
Also, a temporary value (such as an integer literal) cannot be bound to a non-const reference, but can be bound to a const reference parameter or a value parameter.
Code:
int findLowest(int &, int &, int &, int &, int &);// pass by reference
int main()
{
cout << findLowest(2,5,6,23,1);// compile error, this won't work
int val2 = 2, val5 = 5, val6 = 6, val23 = 23, val1 = 1;
cout << findLowest(val2,val5,val6,val23,val1);// but this will
}
Code:
int findLowest(int, int, int, int, int);// pass by value
int main()
{
cout << findLowest(2,5,6,23,1);// this is fine
int val2 = 2, val5 = 5, val6 = 6, val23 = 23, val1 = 1;
cout << findLowest(val2,val5,val6,val23,val1);// this is fine also
}
Re: Find the Lowest of These
ah, ok, i get it now, thanks for the tip