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.
void return type.
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.
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;
}
int number() {
return 50;
}
int main() {
int result = number();
std::cout << result;
return 0;
}
Output:
50
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.int getAge() {
return 21;
}
int main() {
int age = getAge();
std::cout << "Age = "
<< age;
return 0;
}
The function returns an integer value.
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.
char getGrade() {
return 'A';
}
int main() {
char grade = getGrade();
std::cout << "Grade = "
<< grade;
return 0;
}
The function returns a single character.
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.
#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.
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.
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.
int square(int number) {
return number * number;
}
int main() {
std::cout << square(5);
return 0;
}
Output:
25
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.
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.
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.
int test() {
std::cout << "Start";
return 10;
std::cout << "End";
}
The statement after return 10; is not executed because
return ends the function immediately.
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.
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.
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.
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.
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.
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.
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.
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.
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.
return; with returning a value.int add(int a, int b) {
return a + b;
}
#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().
#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;
}
void when no value needs to be returned.double calculateTotal(double price, int quantity) {
return price * quantity;
}
| 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;
}
return statement ends the current function.void function does not return a value.return; can be used to exit a void function early.Question: What does the return statement do in a C++ function?