A return value is the value that a function sends back to the place where the function was called. In C, the return statement is used to send a value back from a function.
A return value is the result produced by a function and sent back to the calling code.
int add()
{
return 10;
}
Here, the function returns the integer value 10.
The return statement sends a value back from a function.
int getNumber()
{
return 50;
}
When the function is called, it produces the value 50.
int getAge()
{
return 20;
}
int main()
{
int age = getAge();
printf("Age = %d", age);
return 0;
}
The function returns an integer value that is stored in the
age variable.
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(10, 20);
printf("Result = %d", result);
return 0;
}
The expression a + b is evaluated and its result is
returned.
float getPrice()
{
return 99.50f;
}
int main()
{
float price = getPrice();
printf("Price = %.2f", price);
return 0;
}
A function declared with float can return a floating-point
value.
double calculateArea(double radius)
{
return 3.14159 * radius * radius;
}
int main()
{
double area = calculateArea(5.0);
printf("%.2f", area);
return 0;
}
The function returns a double value.
char getGrade()
{
return 'A';
}
int main()
{
char grade = getGrade();
printf("Grade = %c", grade);
return 0;
}
A function with return type char can return a character.
int maximum(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int main()
{
printf("%d", maximum(25, 40));
return 0;
}
The function returns the larger of the two numbers.
int minimum(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
int main()
{
printf("%d", minimum(25, 40));
return 0;
}
The function returns the smaller value.
int square(int number)
{
return number * number;
}
int main()
{
int result;
result = square(6);
printf("Square = %d", result);
return 0;
}
A returned value can be stored in a variable for later use.
int square(int number)
{
return number * number;
}
int main()
{
printf("Square = %d", square(5));
return 0;
}
A function's return value can be used directly as an argument to another
function such as printf().
int checkPositive(int number)
{
if (number > 0)
{
return 1;
}
return 0;
}
int main()
{
printf("%d", checkPositive(10));
return 0;
}
A function can return different values depending on a condition.
A function can contain multiple return statements, but only
one of them is executed during a particular function call.
int absoluteValue(int number)
{
if (number < 0)
{
return -number;
}
return number;
}
The first return is used for negative values, while the second is used for zero or positive values.
int checkNumber(int number)
{
if (number > 0)
{
return 1;
}
return 0;
}
The value 0 can be returned to represent a false or
unsuccessful condition in many simple programs.
int isEven(int number)
{
if (number % 2 == 0)
{
return 1;
}
return 0;
}
int main()
{
if (isEven(10))
{
printf("Even");
}
return 0;
}
The function returns 1 when the number is even and
0 otherwise.
int isAdult(int age)
{
return age >= 18;
}
int main()
{
if (isAdult(20))
{
printf("Adult");
}
else
{
printf("Not Adult");
}
return 0;
}
The result of the comparison is returned and then used directly by
the if statement.
int add(int a, int b)
{
return a + b;
}
int main()
{
int result;
result = add(10, 20) * 2;
printf("%d", result);
return 0;
}
A returned value can participate in another expression.
int square(int number)
{
return number * number;
}
int calculate(int number)
{
return square(number) + 10;
}
int main()
{
printf("%d", calculate(5));
return 0;
}
One function can call another function and use its return value.
int calculateSum(int numbers[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += numbers[i];
}
return sum;
}
int main()
{
int numbers[] = {10, 20, 30};
printf("Sum = %d", calculateSum(numbers, 3));
return 0;
}
The function calculates the total and returns the result.
float average(float a, float b, float c)
{
return (a + b + c) / 3.0f;
}
int main()
{
float result;
result = average(80.0f, 70.0f, 90.0f);
printf("Average = %.2f", result);
return 0;
}
The function returns a floating-point average.
int factorial(int number)
{
int result = 1;
for (int i = 1; i <= number; i++)
{
result *= i;
}
return result;
}
int main()
{
printf("Factorial = %d", factorial(5));
return 0;
}
The function calculates the factorial and returns the final result.
#include <stdio.h>
int doubleNumber(int number)
{
return number * 2;
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("Result = %d", doubleNumber(number));
return 0;
}
User input can be passed to a function, processed, and returned as a result.
char getResult(int marks)
{
if (marks >= 40)
{
return 'P';
}
return 'F';
}
int main()
{
printf("Result = %c", getResult(75));
return 0;
}
The function returns either P or F depending
on the marks.
A function with return type void does not return a value to
the caller.
void greet()
{
printf("Hello!");
}
int main()
{
greet();
return 0;
}
The function performs an action but does not produce a return value.
void checkNumber(int number)
{
if (number < 0)
{
return;
}
printf("Positive or zero");
}
In a void function, return; can be used to
exit the function without returning a value.
int multiply(int, int);
int main()
{
int result = multiply(5, 6);
printf("%d", result);
return 0;
}
int multiply(int a, int b)
{
return a * b;
}
The prototype specifies that the function returns an
int.
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int multiply(int a, int b)
{
return a * b;
}
int main()
{
int a = 20;
int b = 5;
printf("Addition = %d\n", add(a, b));
printf("Subtraction = %d\n", subtract(a, b));
printf("Multiplication = %d\n", multiply(a, b));
return 0;
}
#include <stdio.h>
int isEven(int number)
{
return number % 2 == 0;
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isEven(number))
{
printf("Even Number");
}
else
{
printf("Odd Number");
}
return 0;
}
The function returns a value that is directly used by the
if statement.
void function in a variable.return immediately exits the function.int add(int a, int b)
{
return a + b;
}
Always make sure the return type and returned result match the purpose of the function.
Practice the following programs:
Try calling these functions with variables, constants, and user input.
return statement sends a value back to the caller.void function does not return a value.return; can exit a void function without returning a value.Question: Which keyword is used to send a value back from a function in C?