Return type in C++
The outcome of a function is referred to as its return value, and the data type of the return value is referred to as its return type.
Every function declaration and definition must provide a return type, regardless of whether it returns a value.
A function can have a user-defined return type. As an example:
enum count {one, two, three};
enum count counter();
The user-defined type may not be defined within the function declaration.
enum count{one, two, three} counter(); // error in C++
References can also be utilized as function return types. The reference returns the value of the item it refers to.
Return by Address Function in C++:
In this part, we will cover the return type of function: return by address. A function can also return addresses. When a function takes parameters as addresses, it is called a parameter pass-by address. A function in C++ can also return an address. Let's look at various functions that return an address.
Example:
#include <iostream>
using namespace std;
int* fun (int size)
{
int *p = new int[size];
for (int i = 0; i < size; i++)
{
p[i] = i + 1;
}
return p;
}
int main()
{
int *ptr = fun (5);
for (int j = 0; j < 4; j++)
{
cout << ptr[j] << endl;
}
}
Output:
![Return type in C++](https://static.tutorialandexample.com/cpp/return-type-in-cpp1.png)
Return by Address
- A function can return the memory address.
- It shouldn't return the address of local variables that will be discarded once the function is completed.
- It can return the address of the heap memory allocated.
Function Return by Reference in C++
Now, let's take a look at function returns by reference. Let me illustrate this with an example. Please take a look at the piece of code below.
int& fun(int &a){
cout << a << endl;
return a;
}
int main(){
int x = 15;
fun(x) = 25;
cout << x << endl;
}
Within the primary function, we declared a variable x and initialized it with a value of 15. The function 'fun' is then called by providing x. So, this function takes the 'a' integer variable as an argument, which is a reference to the variable 'x'.
We know one thing: when the function returns a value, we can store it in a variable, as in 'int y = fun(x)'. Yes, we may save the return value in a variable so the function is always on the correct side of the assignment. They are not written on the left side.
'a' is a reference to 'x', not a separate variable. As a result, this is referred to as a reference. 'fun' displays 'a'. Thus, it displays 15 and returns 'a'. As a result, the function's return type is 'int &'. It will not return the value ''a, but rather the reference to 'x'. As a result, when it returns here, this function 'fun(x)' becomes nothing more than 'x'.
So, the whole 'fun(x)' thing becomes X. We can write 'fun(x) = 25' here. So, you can see that our function is written on the left-hand side, which is quite impressive. Our function serves as a pointer to the variable 'x'.
Example:
#include <iostream>
using namespace std;
int& fun(int &a)
{
cout << a << endl;
return a;
}
int main()
{
int x = 15;
fun(x) = 25;
cout << x << endl;
}
Output:
![Return type in C++](https://static.tutorialandexample.com/cpp/return-type-in-cpp2.png)