Javatpoint Logo
Javatpoint Logo

Return Statement in C

In this article, we will discuss the return statement in C with its syntax, examples, and different aspects.

Introduction:

The return statement in C terminates the execution of a function and transfers control back to the function that was invoked. Depending on the return type of the function, the return statement might or might not return a value. For instance, the functions int and void both return integer values, respectively.

In C, the return statement can only be used to return a single value from a function, and the data_type of the return value must be declared in the function definition or declaration.

Syntax:

It has the following syntax:

Methods of Return Functions:

Return statements can be used in a variety of ways. Below are a handful of them:

1. Methods that don't return values

When a function's return type is a non-void type, user cannot omit the return statement in C. Only void-type variables can be used without a return statement.

A. Functions of the void return type should not use return statements:

It is not essential to use return when using it because the void function returns an empty value.

Syntax:

It has the following syntax:

Example:

Let's take a simple example to understand the use of the return function in C.

Output:

The sum of 5 and 7 is: 12

B. Applying the return statement to a function with a void return type.

Since void stands for empty, we don't need to return anything, but as the example below illustrates, we can utilize the return statement inside void functions. However, we are still unable to return any results.

Syntax:

It has the following syntax:

This syntax is used as a jump statement to interrupt the flow of the function and exit it. It serves as an alternative to "break statement" that can be used in functions.

Example:

Output:

The number 10 is positive.
The number -5 is negative.
The number is zero. Exiting the function.

However, errors will result if the return statement tries to return a value in a function with a void return type.

Wrong Syntax:

Example:

Output:

error: void function 'Print' should not return a value [-Wreturn-type]
    return 10;
    ^~~~~~
1 error generated.

2. Procedures that return values

The return value of the non-void return type that was provided in the definition and declaration of the function must come right after the return statement.

Syntax:

It has the following syntax:

Example:

Output:

The factorial of 5 is 120

Conclusion:

In summary, the C returns statement is essential for describing how functions with various return types behave. The following details regarding the return statement in C are crucial to keep in mind:

  • Return is a command used to end a function and give the caller back control. It is applicable to both void and non-void functions (those that have return types other than void).
  • The return statement should not contain a return value in a void function. Still, it may be used to cease the execution of the function or to exit the function early under specific circumstances.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA