
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
Return Statement in C++
The return statement in C++ is used to exit a function and to send a value back to the function's caller which is optional depending on requirement. It plays a very important role in controlling the flow of a program and making sure that functions will provide results to other parts of the code.
Syntax
Below is the syntax of using return statement in C++ −
return [expression];
Where, "expression" is optional, specifically used for function. If provided, it specifies the value to be returned to the caller.
Example of return Statement
The following is an example of return statement −
#include <iostream> using namespace std; int sum(int a, int b){ // Here, returning sum of a and b return a + b; } int main(){ // Calling the function int ans = sum(5, 2); cout << "The sum of two integers 5 and 2 is: " << ans << endl; // Returning from the main (), // 0 represents execution done without any error return 0; }
Output
The sum of two integers 5 and 2 is: 7
Key Aspects of return Statement
1. Function Termination
When a return statement is executed, the function exits immediately, and optionally sends value back to the caller.
2. Returning Types
Return by Value
In this the specified value in return statement is sent back to the caller. This is essential for functions which perform calculations or need to provide results.
int Add(int a, int b) { return a + b; // Returns the sum of a and b }
No Return Value (void)
The functions which are declared with void, the return statement can be used without an expression to exit the function early.
void GetMessage() { cout << "Hello, TutorialsPoint Learner!"; return; // Exits the function }
3. Multiple return Statements
A function may consist of multiple return statements, which we generally get to see within conditional statements.
int max(int a, int b) { if (a > b) return a; else return b; }
4. Returning Objects
Functions can return objects, which can be useful for returning multiple values encapsulated in a class or struct.
struct point { int x, y; }; point getOrigin() { return {0, 0}; }
5. Early Exit
The return statement can be used to exit a function early, which is useful for error handling or special conditions.
int divideInteger(int a, int b) { if (b == 0) { cer << "Error: Division by zero!" << endl; return -1; // Shows an error } return a / b; }
Return Types and Value Handling in C++
In C++, the return type of a function determines what kind of value (if any) a function will return to the caller. Proper handling of return types and values is important for making sure that functions behave as expected and integrate smoothly with other parts of the program.
1. Primitive Data Types
Primitive data types are the basic built-in types provided by C++. Common examples are like int, float, double, char etc.
Example
#include <iostream> using namespace std; // created a function which returns an integer int getSquare(int num) { return num * num; } int main() { int value = 5; int result = getSquare(value); // Calling the function and storing its result cout << "The square of " << value << " is " << result << endl; return 0; }
Output
The square of 5 is 25
2. User-Defined Types
User-defined types include structs and classes. These types allow you to define complex data structures and customize them as per your specific requirements.
Example with Struct
#include <iostream> using namespace std; struct Point { int x; int y; }; Point createPoint(int x, int y) { Point p; p.x = x; p.y = y; return p; // will returns a Point object } int main() { Point p = createPoint(10, 20); cout << "Point coordinates: (" << p.x << ", " << p.y << ")" << endl; return 0; }
Output
Point coordinates: (10, 20)
Example with Classes
#include <iostream> using namespace std; class rectangle { public: rectangle(int w, int h) : width(w), height(h) {} int getArea() const { return width * height; } private: int width; int height; }; rectangle createRectangle(int width, int height) { return rectangle(width, height); // Returns a Rectangle object } int main() { rectangle rect = createRectangle(10, 5); cout << "Area of given Rectangle is: " << rect.getArea() << endl; return 0; }
Output
Area of given Rectangle is: 50
3. References and Pointers
References and pointers are used to refer to variables or objects without making any copies. Which can be useful for efficiency and and easy to modify the original data when needed.
Returning by Reference
#include <iostream> using namespace std; int globalValue = 100; int& getGlobalValue() { return globalValue; // Returns a reference to the global variable } int main() { int& ref = getGlobalValue(); ref = 200; // Modifies the global variable cout << "Global Value: " << globalValue << endl; return 0; }
Output
Global Value: 200
Returning by Pointer
#include <iostream> using namespace std; int* createArray(int size) { int* array = new int[size]; // Allocating memory dynamically for (int i = 0; i < size; ++i) { array[i] = i * 10; } return array; // Returning a pointer to the allocated array } int main() { int* myArray = createArray(5); for (int i = 0; i < 5; ++i) { cout << myArray[i] << " "; } delete[] myArray; // Free dynamically allocated memory return 0; }
Output
0 10 20 30 40