C Programs Tutorials | IT Developer
IT Developer

C Programming - C return statement



Share with a Friend

C Programming - C return statement

C return Statement

The return statement in C is used to terminate the execution of a function and optionally return a value to the function caller. It is an essential part of functions in C, especially in functions that have a return type other than void.

Syntax of the return Statement

C

return [expression];

  • expression: The value or result that the function returns. This can be any valid expression of the same type as the return type of the function. If the function has a void return type, no expression is needed.

Types of Return Statements

  1. Returning a Value (Non-void Functions)
    • A function that is designed to return a value must use the return statement to send a value back to the caller.
    • The type of the value must match the declared return type of the function.
  2. No Return Value (Void Functions)
    • For functions that do not return a value (void functions), the return statement can still be used to exit the function early.

Examples of return Statement in C

Example 1: Returning a Value from a Function

C

#include <stdio.h>

 

// Function that returns the sum of two integers

int add(int a, int b) {

    return a + b;  // Returns the sum of a and b

}

 

int main() {

    int result = add(5, 3);  // Calling the add function

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

    return 0;

}

Explanation:

  • The add function returns the sum of two integers (a + b) using the return statement.
  • The value returned by the function (8 in this case) is captured in the result variable in main() and printed.

Output:

Sum: 8

Example 2: Early Exit from a Function (Void Function)

C

#include <stdio.h>

 

// Void function that prints a message and exits early

void printMessage(int num) {

    if (num < 0) {

        printf("Negative number detected. Exiting function.\n");

        return;  // Early exit if the number is negative

    }

    printf("Number is: %d\n", num);

}

 

int main() {

    printMessage(10);  // Prints the number

    printMessage(-5);  // Exits early due to negative number

    return 0;

}

Explanation:

  • The printMessage function checks if the input number is negative. If it is, it uses the return statement to exit the function early.
  • If the number is non-negative, it prints the number.

Output:

Number is: 10

Negative number detected. Exiting function.

Key Points to Remember About return in C

  1. Return Type: If the function has a non-void return type, the return statement must return a value of that type. If the function's return type is void, the return statement does not need to return any value.
  2. Early Return: The return statement can be used at any point in a function to exit early. This is particularly useful in cases like error handling or conditional exits.
  3. Optional for Void Functions: For functions with a void return type, the return statement is optional, but it can still be used to exit the function early.
  4. Function Termination: When a return statement is executed, it not only returns the specified value (if applicable) but also terminates the function's execution, and control is transferred back to the calling function.
  5. No Return in Main: In C, the main() function may return an integer value to indicate the exit status of the program. The return value is typically 0 for successful execution, or a non-zero value to indicate an error. If no return statement is present in main(), it is implicitly assumed to return 0.

Conclusion

The return statement is crucial in C for terminating a function and passing back control to the calling code. In functions with return values, it allows returning a computed value, while in void functions, it can be used to exit the function early. Understanding the return statement is fundamental to working with functions in C.