In C++, function return type is a value returned before a function completes its execution and exits. Let's see some of the most critical points to keep in mind about returning a value from a function.
Returning anything from a function with a void return type, leads to a compile error.
function with a void return types means this function cannot return a value. But, if you are still returning a value from a function with void return type, a compile error will be thrown.
A.cpp
#include<iostream>
using namespace std;
int main()
{
void message(); // function prototype declaration
message(); // calling function message()
}
void message()
{
cout<< "Hello";
return 0; // Returning an int value from a function with void return type
}
Output-
Let's say this program was saved with the name A.cpp. Executing this program has generated the following compile error.
A.cpp: In function 'void message()':
A.cpp:16:8: error: return-statement with a value, in function returning 'void'
[-fpermissive]
return 0; // Returning an int value from a function with void return type
^
Empty return statement in a function with void return type, works fine.
An empty return statement is acceptable within function with void return type because it doesn't return any value.
#include<iostream>
using namespace std;
int main()
{
void message(); // function prototype declaration
message(); // calling function message()
}
void message()
{
cout<< "Hello";
return ; // Returning an int value from a function with void return type
}
Output-
Hello
Larger primitive return type of a function can be used to return a smaller primitive value.
A function with larger primitive return type can return any smaller primitive type values, as long as it doesn't lead to a loss in the value of smaller primitive types.
#include<iostream>
using namespace std;
int main()
{
long funct(); // function prototype declaration
long a = funct();
cout << "Value in a : " << a;
}
long funct()
{
int i = 90;
return i;
}
Output-
Value in a : 90
Here we are returning a long value out of a function that has an int return type, this works because -
An long data type is larger than int, hence int return type is large enough to hold a int value and return it from the function.
There is no loss in the value of int even if its value is widened to an long.
Advertisement
A char value is returned from a function with double return type
#include<iostream>
using namespace std;
int main()
{
double funct(); // function prototype declaration
double d = funct();
cout << "Value in d : " << d;
}
double funct()
{
char c ='A';
return c;
}
Output-
Value in d : 65
Here we are returning a char value out of a function that has a double return type, this works because -
Data type double is larger than char, hence, double return type is large enough to hold a char value and return it from the function.
There is no loss in the value of char even if its value is widened to a double. Hence, ASCII value of A(65) is printed within cout after converting it to a double value, which remains 65 and not 65.00, because the decimal part is only .00, hence it is truncated off.
A floating-point value can be returned from a method with an integer return type.
In the upcoming code, we are going to return a float value from a method with a long return type.
#include
using namespace std;
int main()
{
long return_value(); /*function prototype declaration*/
long l= return_value();
cout << "Value in l is " << l;
}
long return_value()
{
float f = 10.5f;
return f;
}
Output-
Value in l is 10
To return a float out of a function with long return type, the float value must be converted to long. When converting a float value to long,
the decimal value of float will be truncated off, leading to a loss in the value of float, which is allowed by C++ compiler.