Lesson 36 of 60 – Return Values in C
60%

Return Values in C

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.

Note: The return type of a function tells the compiler what type of value the function is expected to return.

1. What is a Return Value?

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.

2. The return Statement

The return statement sends a value back from a function.

int getNumber()
{
    return 50;
}

When the function is called, it produces the value 50.

3. Returning an Integer

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.

4. Returning the Result of a Calculation

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.

5. Returning a Float Value

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.

6. Returning a Double 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.

7. Returning a Character

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.

8. Returning the Larger Number

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.

9. Returning the Smaller Number

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.

10. Storing a Return Value in a Variable

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.

11. Using a Return Value Directly

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().

12. Returning a Value from a Conditional Statement

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.

13. Multiple return Statements

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.

14. Returning Zero

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.

15. Returning 1 or 0

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.

16. Using Return Value in an if Statement

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.

17. Return Value in an Expression

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.

18. Calling One Function from Another

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.

19. Returning the Sum of an Array

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.

20. Returning an Average

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.

21. Returning Factorial

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.

22. Returning a User Input 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.

23. Function Returning a Character

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.

24. void Functions Do Not Return a Value

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.

25. return Without a Value in a void Function

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.

26. Function Prototype with Return Type

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.

27. Complete Calculator with Return Values

#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;
}

28. Complete Even or Odd Program

#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.

29. Common Mistakes with Return Values

  • Using the wrong return type.
  • Returning a value incompatible with the intended function type.
  • Forgetting to return a value from a non-void function when a value is needed.
  • Trying to store the result of a void function in a variable.
  • Forgetting that 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.

30. Practice Programs Using Return Values

Practice the following programs:

  1. Create a function that returns the sum of two numbers.
  2. Create a function that returns the difference of two numbers.
  3. Create a function that returns the product of two numbers.
  4. Create a function that returns the square of a number.
  5. Create a function that returns the cube of a number.
  6. Create a function that returns the larger of two numbers.
  7. Create a function that returns the smaller of two numbers.
  8. Create a function that returns whether a number is even.
  9. Create a function that returns the factorial of a number.
  10. Create a function that returns the average of three numbers.

Try calling these functions with variables, constants, and user input.

📌 Key Points

  • The return statement sends a value back to the caller.
  • The return type specifies the type of value a function returns.
  • A function can return integers, floats, doubles, characters, and other appropriate types.
  • A returned value can be stored in a variable.
  • A returned value can be used directly in an expression.
  • A function can have multiple return statements.
  • Only the executed return statement sends a value back during a function call.
  • A void function does not return a value.
  • return; can exit a void function without returning a value.
  • Return values make functions useful for calculations and decision-making.

🧠 Quick Quiz

Question: Which keyword is used to send a value back from a function in C?