100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

return Statement in C++

Understand how the return statement ends a function and optionally sends a value back to the caller, including void functions.

FunctionsBeginner6 min readJul 7, 2026
Analogies

1. Intro

The return statement immediately ends the execution of a function and, optionally, sends a value back to the code that called it. What the return statement is allowed to send back depends entirely on the function's declared return type.

🏏

Cricket analogy: When an umpire signals "out", play stops immediately for that batter and the decision is sent back to the scorer, just as return immediately ends a function and can send a value back to the caller, matching what the function promised to deliver.

2. Syntax

cpp
returnType functionName(parameterList) {
    // statements
    return value;   // value's type must match returnType
}

// for a function with no return value:
void functionName(parameterList) {
    // statements
    return;   // optional, just exits the function
}

3. Explanation

When a function's return type is something like int, double, or string, its return statement must supply a value of a compatible type — this value is handed back to the caller and can be stored, printed, or used in an expression. When a function is declared void, it means the function does not return any value; its return statement (if used at all) takes no value and is simply used to exit the function early, for example inside an if block. Execution of a function stops the instant a return statement runs — any code written after it in the same block is never executed. A function can contain multiple return statements (e.g., in different branches of an if-else), but only one of them executes on any given call.

🏏

Cricket analogy: A function returning int must hand back a real run total like return 87, usable by the caller; a void function like announceOver() just exits early with a bare return inside an if, and once a boundary-hit branch returns, no code after it in that over's logic runs — multiple returns can exist across different innings-state branches, but only one fires per call.

A non-void function must return a value on every possible execution path. If a code path in a value-returning function has no return statement, the behavior is undefined and most compilers will warn you — always ensure every branch returns something.

4. Example

cpp
#include <iostream>
using namespace std;

int checkSign(int n) {
    if (n > 0) {
        return 1;      // positive
    } else if (n < 0) {
        return -1;      // negative
    }
    return 0;           // zero
}

void printGreeting(bool morning) {
    if (!morning) {
        cout << "Good evening!" << endl;
        return;          // exits early, no value returned
    }
    cout << "Good morning!" << endl;
}

int main() {
    cout << "Sign of -7: " << checkSign(-7) << endl;
    printGreeting(false);
    return 0;
}

5. Output

text
Sign of -7: -1
Good evening!

6. Key Takeaways

  • The return statement ends a function's execution immediately.
  • A function's return type determines what kind of value (if any) return can send back.
  • A void function returns no value; its return statement (if present) takes no value.
  • Code written after a return statement in the same block never executes.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#ReturnStatementInC#Return#Statement#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep