C Function Return Types
In C, functions can return different types of values based on their declared return type. The return type of a function specifies the type of data that the function will send back to the caller. If a function does not return a value, its return type is declared as void
. Understanding function return types is crucial for designing functions that interact properly with the rest of your program.
Example 1: Function with int
Return Type
This example demonstrates a function that calculates the sum of two integers and returns an int
value.
main.c
#include <stdio.h>
// Function declaration: returns an int and takes two int parameters
int add(int a, int b) {
return a + b; // Return the sum of a and b
}
int main() {
int result = add(10, 20); // Call the add function and store the returned value
printf("The sum is: %d\n", result);
return 0;
}
Explanation:
- The function
add
is declared to return anint
and accepts twoint
parameters. - Inside
add
, the sum ofa
andb
is calculated and returned to the caller. - In
main()
, the returned value is stored in the variableresult
and printed usingprintf()
.
Output:
The sum is: 30
Example 2: Function with void
Return Type
This example shows a function that performs an action (printing a message) without returning any value. The return type void
indicates that the function does not return data to the caller.
main.c
#include <stdio.h>
// Function declaration: returns void and takes no parameters
void greet() {
printf("Hello from the greet function!\n");
}
int main() {
greet(); // Call the greet function
return 0;
}
Explanation:
- The function
greet
is declared with a return type ofvoid
, meaning it does not return any value. - Inside
greet
, a simple message is printed usingprintf()
. - In
main()
, thegreet
function is called to display the message.
Output:
Hello from the greet function!
Example 3: Function with float
Return Type
This example demonstrates a function that calculates the average of two floating-point numbers and returns a float
value.
main.c
#include <stdio.h>
// Function declaration: returns a float and takes two float parameters
float average(float a, float b) {
return (a + b) / 2.0; // Return the average of a and b
}
int main() {
float avg = average(5.5, 10.5); // Call the average function and store the returned value
printf("The average is: %f\n", avg);
return 0;
}
Explanation:
- The function
average
is declared to return afloat
and takes twofloat
parameters. - Inside the function, the sum of
a
andb
is divided by2.0
to compute the average. - The computed average is returned to the caller.
- In
main()
, the returned value is stored in the variableavg
and printed usingprintf()
.
Output:
The average is: 8.000000
Conclusion
In this tutorial, we explored the concept of function return types in C. We learned:
int
Return Type: Functions that return integer values, as shown by ouradd
function.void
Return Type: Functions that perform an action without returning a value, like thegreet
function.float
Return Type: Functions that return floating-point numbers, demonstrated by ouraverage
function.