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

</>
Copy
#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:

  1. The function add is declared to return an int and accepts two int parameters.
  2. Inside add, the sum of a and b is calculated and returned to the caller.
  3. In main(), the returned value is stored in the variable result and printed using printf().

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

</>
Copy
#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:

  1. The function greet is declared with a return type of void, meaning it does not return any value.
  2. Inside greet, a simple message is printed using printf().
  3. In main(), the greet 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

</>
Copy
#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:

  1. The function average is declared to return a float and takes two float parameters.
  2. Inside the function, the sum of a and b is divided by 2.0 to compute the average.
  3. The computed average is returned to the caller.
  4. In main(), the returned value is stored in the variable avg and printed using printf().

Output:

The average is: 8.000000

Conclusion

In this tutorial, we explored the concept of function return types in C. We learned:

  1. int Return Type: Functions that return integer values, as shown by our add function.
  2. void Return Type: Functions that perform an action without returning a value, like the greet function.
  3. float Return Type: Functions that return floating-point numbers, demonstrated by our average function.