C++ return Statement

Introduction

The return statement in C++ is used to exit a function and optionally return a value to the calling function. It is an essential part of function control flow, allowing functions to provide output to their callers and to terminate their execution. Understanding the return statement is crucial for writing effective and functional code.

Syntax

The basic syntax of the return statement is as follows:

return;          // For functions that do not return a value (void functions)
return value;    // For functions that return a value

Key Points

  • The return statement ends the execution of a function.
  • If the function returns a value, the return statement is followed by the value to be returned.
  • In void functions, the return statement is used without any value to exit the function.

Example: Basic return Statement in Functions

Example 1: Function with Return Value

In this example, we define a function that returns the sum of two integers.

#include <iostream>
using namespace std;

// Function that returns the sum of two integers
int add(int a, int b) {
    return a + b; // Return the sum of a and b
}

int main() {
    int result = add(5, 3); // Call the function and store the result
    cout << "The sum is: " << result << endl; // Output the result
    return 0;
}

Output

The sum is: 8

Explanation

  • The add function takes two integers as parameters and returns their sum.
  • The return a + b; statement returns the sum of a and b to the caller.
  • The main function calls add(5, 3) and stores the result in result, which is then printed.

Example 2: Function without Return Value (void)

In this example, we define a void function that prints a message.

#include <iostream>
using namespace std;

// Function that prints a message
void printMessage() {
    cout << "Hello, World!" << endl;
    return; // Optional, as the function will return automatically at the end
}

int main() {
    printMessage(); // Call the function
    return 0;
}

Output

Hello, World!

Explanation

  • The printMessage function does not return any value, so it is defined with a void return type.
  • The return; statement is optional in void functions, as the function will return automatically at the end.

Example: Early Return from Function

In this example, we use the return statement to exit a function early based on a condition.

#include <iostream>
using namespace std;

// Function that checks if a number is positive
bool isPositive(int number) {
    if (number <= 0) {
        return false; // Early return if the number is not positive
    }
    return true; // Return true if the number is positive
}

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;

    if (isPositive(number)) {
        cout << "The number is positive." << endl;
    } else {
        cout << "The number is not positive." << endl;
    }

    return 0;
}

Output

Enter a number: 5
The number is positive.

or

Enter a number: -3
The number is not positive.

Explanation

  • The isPositive function checks if the input number is positive.
  • If the number is not positive, the function returns false early.
  • If the number is positive, the function returns true.

Example: Using Return Values for Calculation

In this example, we use multiple functions with return values to perform a calculation.

#include <iostream>
using namespace std;

// Function that returns the square of a number
int square(int x) {
    return x * x; // Return the square of x
}

// Function that returns the cube of a number
int cube(int x) {
    return x * x * x; // Return the cube of x
}

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;

    int numberSquare = square(number); // Call square function
    int numberCube = cube(number); // Call cube function

    cout << "Square of " << number << " is: " << numberSquare << endl;
    cout << "Cube of " << number << " is: " << numberCube << endl;

    return 0;
}

Output

Enter a number: 3
Square of 3 is: 9
Cube of 3 is: 27

Explanation

  • The square function returns the square of the input number.
  • The cube function returns the cube of the input number.
  • The main function calls both functions and prints the results.

Conclusion

The return statement is a fundamental part of C++ functions, allowing you to exit a function and optionally return a value to the calling function. It is used to control the flow of function execution and to provide output from functions. This chapter covered the syntax of the return statement, examples of functions with and without return values, early returns, and using return values for calculations. Understanding how to use the return statement effectively will help you write more functional and modular code. In the next chapter, we will explore the concept of arrays in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top