Lesson 36 of 60 – Return Values in C++
60%

Return Values in C++

A return value is a value sent back from a function to the code that called it. Return values allow functions to calculate, process, or retrieve information and give the result back to the calling code.

Note: The return type of a function tells C++ what type of value the function will return. A function that does not return a value commonly uses the void return type.

1. What is a Return Value?

A return value is the result sent from a function back to the place where the function was called.

int getNumber() {

    return 100;

}

Here, the function returns the value 100.

2. The return Statement

The return statement sends a value back to the calling code and immediately ends the current function.

return value;

Example:

int add() {

    return 10 + 20;

}

3. Simple Return Value Example

int number() {

    return 50;

}

int main() {

    int result = number();

    std::cout << result;

    return 0;
}

Output:

50

4. Return Type

The return type is written before the function name.

int add(int a, int b) {
    return a + b;
}

Here:

  • int is the return type.
  • add is the function name.
  • a and b are parameters.
  • a + b is the returned value.

5. Returning an Integer

int getAge() {

    return 21;

}

int main() {

    int age = getAge();

    std::cout << "Age = "
              << age;

    return 0;
}

The function returns an integer value.

6. Returning a Double

double getPrice() {

    return 99.50;

}

int main() {

    double price = getPrice();

    std::cout << "Price = "
              << price;

    return 0;
}

A function can return decimal values using a suitable floating-point return type.

7. Returning a Character

char getGrade() {

    return 'A';

}

int main() {

    char grade = getGrade();

    std::cout << "Grade = "
              << grade;

    return 0;
}

The function returns a single character.

8. Returning a Boolean Value

bool isEven(int number) {

    return number % 2 == 0;

}

int main() {

    bool result = isEven(10);

    std::cout << result;

    return 0;
}

The function returns either true or false.

9. Returning a String

#include <iostream>
#include <string>

std::string getName() {

    return "Rahul";

}

int main() {

    std::string name = getName();

    std::cout << name;

    return 0;
}

A function can return a string using std::string.

10. Returning the Result of an Expression

int calculate() {

    int a = 10;
    int b = 20;

    return a + b;

}

int main() {

    std::cout << calculate();

    return 0;
}

A return statement can return the result of an expression.

11. Return Value with Parameters

int add(int a, int b) {

    return a + b;

}

int main() {

    int result = add(10, 20);

    std::cout << "Sum = "
              << result;

    return 0;
}

The function receives two values and returns their sum.

12. Returning a Square

int square(int number) {

    return number * number;

}

int main() {

    std::cout << square(5);

    return 0;
}

Output:

25

13. Returning the Larger Number

int maximum(int a, int b) {

    if (a > b) {
        return a;
    }

    return b;
}

int main() {

    std::cout << maximum(25, 40);

    return 0;
}

The function returns the larger of the two numbers.

14. Multiple Return Statements

A function can contain multiple return statements when different conditions produce different results.

int checkNumber(int number) {

    if (number > 0) {
        return 1;
    }

    if (number < 0) {
        return -1;
    }

    return 0;
}

Only one return statement is reached during a particular function call.

15. Return from an if Statement

int checkAge(int age) {

    if (age >= 18) {
        return 1;
    }

    return 0;
}

int main() {

    std::cout << checkAge(20);

    return 0;
}

When the condition is true, the function returns 1 immediately.

16. Return Statement Ends the Function

int test() {

    std::cout << "Start";

    return 10;

    std::cout << "End";
}

The statement after return 10; is not executed because return ends the function immediately.

17. Return Value Stored in a Variable

int multiply(int a, int b) {

    return a * b;

}

int main() {

    int result = multiply(5, 6);

    std::cout << "Result = "
              << result;

    return 0;
}

The returned value can be stored in a variable for later use.

18. Return Value Used Directly

int add(int a, int b) {

    return a + b;

}

int main() {

    std::cout << add(10, 20);

    return 0;
}

A returned value can also be used directly as part of another expression or output statement.

19. Returning from a void Function

A void function does not return a value. However, it can use return; without a value to exit the function early.

void check(int number) {

    if (number < 0) {
        return;
    }

    std::cout << "Positive number";
}

Here, return; simply exits the function.

20. Returning Factorial

int factorial(int number) {

    int result = 1;

    for (int i = 1; i <= number; i++) {

        result *= i;
    }

    return result;
}

int main() {

    std::cout << factorial(5);

    return 0;
}

The function calculates the factorial and returns the final result.

21. Returning Average

double average(double a, double b, double c) {

    return (a + b + c) / 3;

}

int main() {

    double result = average(70, 80, 90);

    std::cout << "Average = "
              << result;

    return 0;
}

The function returns a decimal average.

22. Returning a Grade

char getGrade(int marks) {

    if (marks >= 90) {
        return 'A';
    }
    else if (marks >= 75) {
        return 'B';
    }
    else if (marks >= 60) {
        return 'C';
    }
    else if (marks >= 40) {
        return 'D';
    }

    return 'F';
}

int main() {

    std::cout << "Grade = "
              << getGrade(85);

    return 0;
}

The function returns different characters depending on the marks.

23. Returning a Boolean Result

bool isPositive(int number) {

    return number > 0;

}

int main() {

    if (isPositive(25)) {

        std::cout << "Positive";

    }

    return 0;
}

A boolean return value is useful when a function answers a yes-or-no type question.

24. Using One Function's Return Value in Another

int square(int number) {

    return number * number;

}

int add(int a, int b) {

    return a + b;

}

int main() {

    int result = add(square(2), square(3));

    std::cout << result;

    return 0;
}

A returned value can be passed directly as an argument to another function.

25. Returning User Input Result

int getNumber() {

    int number;

    std::cout << "Enter a number: ";
    std::cin >> number;

    return number;
}

int main() {

    int value = getNumber();

    std::cout << "You entered: "
              << value;

    return 0;
}

A function can collect input and return the entered value.

26. Common Mistakes with Return Values

  • Using the wrong return type.
  • Forgetting to return a value from a non-void function.
  • Returning a value that is not appropriate for the function's return type.
  • Writing code after a return statement and expecting it to execute.
  • Confusing return; with returning a value.
  • Ignoring a useful returned value.
  • Expecting a void function to return a value.
int add(int a, int b) {

    return a + b;
}

27. Practical Calculator Return Values

#include <iostream>

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}

int main() {

    int x = 20;
    int y = 5;

    std::cout << "Addition = "
              << add(x, y)
              << std::endl;

    std::cout << "Subtraction = "
              << subtract(x, y)
              << std::endl;

    std::cout << "Multiplication = "
              << multiply(x, y);

    return 0;
}

Each function calculates a result and returns it to main().

28. Practical Student Result

#include <iostream>

char getGrade(int marks) {

    if (marks >= 90) {
        return 'A';
    }

    if (marks >= 75) {
        return 'B';
    }

    if (marks >= 60) {
        return 'C';
    }

    if (marks >= 40) {
        return 'D';
    }

    return 'F';
}

bool isPassed(int marks) {

    return marks >= 40;
}

int main() {

    int marks;

    std::cout << "Enter marks: ";
    std::cin >> marks;

    std::cout << "Grade = "
              << getGrade(marks)
              << std::endl;

    if (isPassed(marks)) {
        std::cout << "Result: Pass";
    }
    else {
        std::cout << "Result: Fail";
    }

    return 0;
}

29. Best Practices for Return Values

  • Choose a return type that matches the result.
  • Give the function a meaningful name.
  • Return a clear and useful result.
  • Make sure all required execution paths return an appropriate value.
  • Use void when no value needs to be returned.
  • Use boolean return values for true/false decisions.
  • Avoid unnecessary return statements.
  • Keep return logic easy to understand.
double calculateTotal(double price, int quantity) {

    return price * quantity;

}

30. Return Values – Final Summary

Concept Meaning
return Sends a value back to the calling code and ends the function.
Return Type Specifies the type of value a function returns.
int Can return an integer value.
double Can return a decimal value.
char Can return a character.
bool Can return true or false.
std::string Can return text.
void Indicates that the function does not return a value.
returnType functionName(parameters) {

    return value;

}

📌 Key Points

  • A return value is a value sent back from a function.
  • The return statement ends the current function.
  • The function's return type specifies what type of value it returns.
  • A function can return integers, decimals, characters, strings, and boolean values.
  • A function can contain multiple return statements when different conditions require different results.
  • Code after an executed return statement is not executed.
  • A void function does not return a value.
  • return; can be used to exit a void function early.
  • A returned value can be stored in a variable or used directly.
  • The returned value of one function can be passed to another function.

🧠 Quick Quiz

Question: What does the return statement do in a C++ function?