- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Return Types
In C++, every function has a return type that defines the type of value it will return after
executing its logic. The return type is specified before the function name and can be any valid C++ data type.
If a function does not return any value, it uses void as its return type.
1. Basic Return Types
-
Primitive Data Types: Functions can return basic types like
int,float,double,char, etc.- Example of an
intReturn Type:cppint add(int a, int b) { return a + b; // Returns the sum of a and b } int main() { int result = add(5, 3); std::cout << "Result: " << result << std::endl; // Output: 8 return 0; }
- Example of an
-
voidReturn Type: A function with thevoidreturn type does not return a value.- Example of a
voidReturn Type:cppvoid printMessage() { std::cout << "Hello, World!" << std::endl; // No return statement } int main() { printMessage(); return 0; }
- Example of a
-
boolReturn Type: Functions can return boolean values (trueorfalse).- Example of a
boolReturn Type:cppbool isEven(int number) { return (number % 2 == 0); // Returns true if the number is even } int main() { bool result = isEven(4); std::cout << "Is even: " << std::boolalpha << result << std::endl; // Output: true return 0; }
- Example of a
2. Returning Objects
Functions in C++ can also return objects of user-defined types such as classes or structs.
-
Example of Returning an Object:
cppclass Point { public: int x, y; Point(int xPos, int yPos) : x(xPos), y(yPos) {} }; Point getPoint() { return Point(10, 20); // Returns an object of type Point } int main() { Point p = getPoint(); std::cout << "Point coordinates: (" << p.x << ", " << p.y << ")" << std::endl; return 0; }
3. Returning Pointers
Functions can return pointers to variables or objects. However, care must be taken to avoid returning pointers to local variables, as they go out of scope once the function ends.
-
Example of Returning a Pointer:
cppint* getPointerToValue() { static int value = 42; // Static variable is preserved after function call return &value; // Return the address of the variable } int main() { int* ptr = getPointerToValue(); std::cout << "Pointer value: " << *ptr << std::endl; // Output: 42 return 0; } -
Important: Avoid returning pointers to local (non-static) variables, as they will be destroyed after the function ends.
4. Returning References
C++ functions can also return references to variables or objects, allowing the caller to directly modify the referred-to value.
-
Example of Returning a Reference:
cppint& getRef(int& a) { return a; // Return reference to the passed-in argument } int main() { int x = 10; int& ref = getRef(x); ref = 20; // Modifies x directly std::cout << "x: " << x << std::endl; // Output: x: 20 return 0; } -
Important: Similar to pointers, avoid returning references to local variables that go out of scope.
5. Returning Arrays
In C++, you cannot return entire arrays from functions directly. However, you can return pointers to arrays or
use structures or std::array/std::vector to handle arrays.
-
Example of Returning a Pointer to an Array:
cppint* getArray() { static int arr[5] = {1, 2, 3, 4, 5}; // Static array remains in memory return arr; // Return the base address of the array } int main() { int* arr = getArray(); for (int i = 0; i < 5; ++i) { std::cout << arr[i] << " "; // Output: 1 2 3 4 5 } return 0; } -
Example of Returning
std::array:cpp#includestd::array<int, 5> getArray() { return {1, 2, 3, 4, 5}; // Return an std::array } int main() { std::array<int, 5> arr = getArray(); for (int i : arr) { std::cout << i << " "; // Output: 1 2 3 4 5 } return 0; } -
Example of Returning
std::vector:cpp#includestd::vector<int> getVector() { return {1, 2, 3, 4, 5}; // Return an std::vector } int main() { std::vector<int> vec = getVector(); for (int i : vec) { std::cout << i << " "; // Output: 1 2 3 4 5 } return 0; }
6. Returning Multiple Values
C++ does not directly support returning multiple values from a function, but you can achieve this by using:
-
std::pairorstd::tuple:-
Example Using
std::pair:cpp#includestd::pair<int, int> getCoordinates() { return std::make_pair(10, 20); // Return a pair of values } int main() { std::pair<int, int> coords = getCoordinates(); std::cout << "X: " << coords.first << ", Y: " << coords.second << std::endl; return 0; } -
Example Using
std::tuple:cpp#includestd::tuple<int, int, int> getRGB() { return std::make_tuple(255, 0, 0); // Return a tuple of values } int main() { auto [r, g, b] = getRGB(); // Structured binding (C++17) std::cout << "R: " << r << ", G: " << g << ", B: " << b << std::endl; return 0; }
-
-
Using
struct:- Example Using
struct:cppstruct Point { int x, y; }; Point getPoint() { return {10, 20}; // Return a struct with two values } int main() { Point p = getPoint(); std::cout << "X: " << p.x << ", Y: " << p.y << std::endl; return 0; }
- Example Using
Summary
- C++ functions can return various types of data, including primitive types, objects, pointers, references,
arrays (via pointers), and containers like
std::arrayorstd::vector. - Multiple values can be returned using
std::pair,std::tuple, or user-definedstructs. - Always ensure that the values being returned remain in valid memory, particularly when returning pointers or references.