return Statement
void printChars(char c, int count) {
for (int i=0; i<count; i++) {
cout << c;
}//end for
return; // Optional because it's a void function
}//end printChars
The max function below requires one or more return statements because it returns an int value.
// Multiple return statements often increase complexity.
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}//end max
Here is a version of the max function that uses only one return statement
by saving the result in a local variable. Some authors insist on only
one return statement at the end of a function.
Readable code is much more important than following such a fixed rule.
The use of a single return probably improves the clarity
of the max function slightly.
// Single return at end often improves readability.
int max(int a, int b) {
int maxval;
if (a > b) {
maxval = a;
} else {
maxval = b;
}
return maxval;
}//end max