Common Mistakes and Correct Usage of return Statements in C++
1. Using return to Return a Value from a Function
1) return 0 in the main Function
#include <iostream>
using namespace std;
int main() {
int input_num;
cout << "Enter a number: ";
cin >> input_num;
cout << "You entered: " << input_num << endl;
return 0;
}
The return value of main() indicates the program's exit status. Returning 0 signals that the program executed successfully without errors. Returning a non-zero integer (e.g., 1) indicates an abnormal termination, such as an invalid input or runtime error.
2) Returning a Constant from a Subfunction
#include <iostream>
using namespace std;
int get_fixed_value(int val) {
return 7; // Returns constant value 7 regardless of input
}
int main() {
int user_input;
cout << "Enter any number: ";
cin >> user_input;
cout << "Function returns: " << get_fixed_value(user_input) << endl;
return 0;
}
This function always returns the constant value 7, regardless of the input parameter. In the main() function, the call to get_fixed_value(user_input) is replaced with the returned value 7 during execution.
3) Returning a Modified Variable from a Subfunction
#include <iostream>
using namespace std;
int adjust_value(int num) {
num += 5; // Increment input by 5
return num;
}
int main() {
int input;
cout << "Enter a number to adjust: ";
cin >> input;
cout << "Adjusted value: " << adjust_value(input) << endl;
return 0;
}
Here, the subfunction modifies the input parameter by adding 5, then returns the updated value. This modified value replaces the function call in main() when executed. The same logic applies to returning expressions like num + 3 or a + b where a and b are variables in the subfunction.
2. Using return with Assignments or Conditional Expressions
1) return with an Assignment Operation
In an int-type Subfunction
#include <iostream>
using namespace std;
int scale_number(int num) {
return num *= 3; // Multiply num by 3 and return the result
}
int main() {
int value;
cout << "Enter a number to scale: ";
cin >> value;
cout << "Scaled result: " << scale_number(value) << endl;
return 0;
}
When using an assignment operation in the return statement of an int-type function, the assignment is fully executed first. The resulting value of the variable after the assignment is then returned as the function's output.
In a bool-type Subfunction
#include <iostream>
using namespace std;
bool is_valid(int input_val) {
return input_val -= 1; // Returns the value after subtraction
}
int main() {
int test_val;
cout << "Enter a test number: ";
cin >> test_val;
cout << "Function returns (1 for true, 0 for false): " << is_valid(test_val) << endl;
return 0;
}
For bool-type funcsions, the return value is enterpreted as true (1) if non-zero and false (0) if zero. The result of the assignment operation (the value assigned to the variable) determines the boolean output. For example, if test_val is 1, input_val becomes 0, so the function returns false (0); any other input will produce a non-zero result, returning true (1).
2) return with a Conditional Expression
#include <iostream>
using namespace std;
int check_divisible_by_three(int num) {
return num % 3 == 0; // Returns 1 if divisible, 0 otherwise
}
int main() {
int check_num;
cout << "Enter a number to check divisibility by 3: ";
cin >> check_num;
cout << "Result (1 for yes, 0 for no): " << check_divisible_by_three(check_num) << endl;
return 0;
}
#include <iostream>
using namespace std;
bool is_divisible_by_three(int num) {
return num % 3 == 0; // Returns true if divisible, false otherwise
}
int main() {
int check_num;
cout << "Enter a number to check divisibility by 3: ";
cin >> check_num;
cout << "Result (1 for true, 0 for false): " << is_divisible_by_three(check_num) << endl;
return 0;
}
When using a conditional expression (like num % 3 == 0) in a return statement, the expression evaluates to a boolean result (true or false). For int-type functions, this boolean is converted to 1 (true) or 0 (false). For bool-type functions, the boolean value is returned directly, which is displayed as 1 or 0 in output.