In C language, function return type is the 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.
/* Return type example in C Language */
#include<stdio.h>
int main()
{
void message(); /*function prototype declaration*/
message(); /* calling function message() */
}
void message()
{
printf("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 functions.c. Executing this program has generated the following compile error.
functions.c: In function 'message':
functions.c:13:8: warning: 'return' with a value, in function returning void
return 0; /*Returning an int value from a function with void return type */
^
functions8.c:10:6: note: declared here
void message()
^~~~~~~
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<stdio.h>
int main()
{
void message(); /*function prototype declaration*/
message(); /* calling function message() */
}
void message()
{
printf("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<stdio.h>
int main()
{
long funct(); /*function prototype declaration*/
long a = funct();
printf("Value in a : %d ", a);
}
long funct()
{
int i = 90;
return i;
}
Output-
Value in a : 90
Here we are returning an int value out of a function named funct(), which has an long return type, this works because -
An long data type is larger than int, hence the long 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.
A char value is returned from a function with double return type
#include<stdio.h>
int main()
{
double funct(); /*function prototype declaration*/
double d = funct();
printf("Value in d : %lf ", a);
}
double funct()
{
char c ='A';
return c;
}
Output-
Value in d : 65.000000
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 by printf() function after converting it to a double value.
Advertisement
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<stdio.h>
int main()
{
long return_value(); /*function prototype declaration*/
long l= return_value();
printf("Value in l is %ld", 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(this is not allowed in Java language and leads to a compiler error in Java).