Skip to main content

Returning data from function in C

Returning data from C function

  • In C, a function can return data to the calling code by using the return statement.

  • The return statement takes a value of the specified return type and returns it to the calling code.

  • The syntax for returning a value from a function in C is as follows:

return expression;

Here, expression is any valid C expression that evaluates to a value of the specified return type.

For example:

int square(int x) {
return x * x;
}

Explanation

  • The function square takes an integer argument x and returns its square, which is calculated as x * x. The return statement returns the value of x * x back to the calling code.

  • The return type of a function is specified in the function signature, and it determines the type of value that the function can return.

For example:

  • The function below with a return type of int can only return integer values
int sum(parameters);
  • The function below with a return type of float can only return floating-point values.
float sum(parameters);
  • If a function does not return a value, it should have a return type of void.
void sum(parameters);
multiple return

It is important to note that a function can return only one value at a time.

If a function needs to return multiple values, it can either return an array or a structure that contains multiple values, or it can use pointers or references to modify the values of variables in the calling code.

Example 1: Returning int type data

#include <stdio.h>

int max(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}

int main()
{
int x = 5;
int y = 10;
int z = max(x, y);
printf("The maximum of %d and %d is %d\n", x, y, z);
return 0;
}
Output:

Explanation

  • The function max takes two integer arguments a and b and returns the maximum of the two values.

  • The main function calls the max function with the arguments x and y, and the result is assigned to the variable z. The value of z is then printed to the console.

Example 2: Returning float type data

#include <stdio.h>

float average(float a, float b)
{
return (a + b) / 2;
}

int main()
{
float x = 5.0;
float y = 10.0;
float z = average(x, y);
printf("The average of %.2f and %.2f is %.2f\n", x, y, z);
return 0;
}
Output:

Explanation

  • The function average takes two floating-point arguments a and b and returns their average, which is calculated as (a + b) / 2. The function uses the return statement to return the value of (a + b) / 2 back to the calling code.

  • The main function calls the average function with the arguments x and y, and the result is assigned to the variable z. The value of z is then printed to the console, rounded to 2 decimal places.

Example 3: Returning void type data

#include <stdio.h>

void print_hello()
{
printf("Hello!\n");
}

int main()
{
print_hello();
return 0;
}
Output:

Explanation

  • The function print_hello is declared to return void data type, which means it does not return any value. The function simply prints the message "Hello!" to the standard output.

  • In the main function, the print_hello function is called, and the message "Hello!" is displayed.