C return Statement

Introduction

In the previous chapters, we explored various control flow statements in C, such as loops and the break statement. In this chapter, we will focus on the return statement. The return statement is used to terminate the execution of a function and optionally return a value to the calling function. It is an essential part of function handling in C.

What is a return Statement?

The return statement is used to exit from a function and return control to the calling function. When a return statement is executed, the function terminates immediately, and control is transferred back to the point where the function was called. If the function is designed to return a value, the return statement can also pass that value back to the calling function.

Syntax

The basic syntax of a return statement in C is as follows:

return; // For functions with a void return type
return expression; // For functions that return a value
  • expression: This is the value that will be returned to the calling function. The type of the expression must match the return type of the function.

Example: Using return in a Function

Let’s look at a simple example to understand how the return statement works in a function.

Example: Function that Adds Two Numbers

#include <stdio.h>

// Function prototype
int add(int a, int b);

int main() {
    int num1 = 5;
    int num2 = 10;
    int sum;

    // Calling the add function
    sum = add(num1, num2);

    // Printing the result
    printf("Sum: %d\n", sum);

    return 0; // Returning 0 to indicate successful execution
}

// Function definition
int add(int a, int b) {
    return a + b; // Returning the sum of a and b
}

Output:

Sum: 15

In this example, the add function returns the sum of its two parameters, which is then printed in the main function.

Returning Different Types

The return statement can be used to return different types of values, such as integers, floating-point numbers, characters, and even pointers.

Example: Returning an Integer

#include <stdio.h>

// Function prototype
int getMax(int a, int b);

int main() {
    int num1 = 20;
    int num2 = 10;
    int max;

    // Calling the getMax function
    max = getMax(num1, num2);

    // Printing the result
    printf("Max: %d\n", max);

    return 0; // Returning 0 to indicate successful execution
}

// Function definition
int getMax(int a, int b) {
    if (a > b) {
        return a; // Returning a if a is greater than b
    } else {
        return b; // Returning b otherwise
    }
}

Output:

Max: 20

Example: Returning a Floating-Point Number

#include <stdio.h>

// Function prototype
float getAverage(float a, float b);

int main() {
    float num1 = 20.5;
    float num2 = 10.5;
    float avg;

    // Calling the getAverage function
    avg = getAverage(num1, num2);

    // Printing the result
    printf("Average: %.2f\n", avg);

    return 0; // Returning 0 to indicate successful execution
}

// Function definition
float getAverage(float a, float b) {
    return (a + b) / 2; // Returning the average of a and b
}

Output:

Average: 15.50

Example: Returning a Character

#include <stdio.h>

// Function prototype
char getGrade(int marks);

int main() {
    int marks = 85;
    char grade;

    // Calling the getGrade function
    grade = getGrade(marks);

    // Printing the result
    printf("Grade: %c\n", grade);

    return 0; // Returning 0 to indicate successful execution
}

// Function definition
char getGrade(int marks) {
    if (marks >= 90) {
        return 'A'; // Returning 'A' if marks are 90 or above
    } else if (marks >= 80) {
        return 'B'; // Returning 'B' if marks are 80 or above
    } else if (marks >= 70) {
        return 'C'; // Returning 'C' if marks are 70 or above
    } else {
        return 'D'; // Returning 'D' otherwise
    }
}

Output:

Grade: B

Using return in void Functions

For functions with a void return type, the return statement is used to exit the function without returning a value.

Example: Using return in a void Function

#include <stdio.h>

// Function prototype
void printMessage();

int main() {
    // Calling the printMessage function
    printMessage();

    return 0; // Returning 0 to indicate successful execution
}

// Function definition
void printMessage() {
    printf("Hello, World!\n");
    return; // Exiting the function
}

Output:

Hello, World!

Conclusion

The return statement is a crucial part of function handling in C. It allows you to terminate a function and optionally return a value to the calling function. By understanding and using the return statement effectively, you can create more modular and reusable code. The ability to return different types of values and control the flow of your functions makes the return statement an essential tool in your C programming toolkit.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top