The return statement in C

The return statement is used to return some value or simply pass the control to the calling function. The return statement can be used in the following two ways.

  1. return;
  2. return expression;

The first form of the return statement is used to terminate the function and pass the control to the calling function. No value from the called function is returned when this form of the return statement is used.

The following program demonstrates the use of the first form of the return statement.

Expected Output:

1st run:

2nd run:

3rd run:

How it works

Let’s say the user entered 17 (value of variable n), then eligible_or_not() function is called, this function expects one argument of type int, which we have correctly supplied as n. The value of variable n is assigned to variable age . Remember age is a local variable, and thus only available inside eligible_or_not() function. if condition (age >= 18) is tested, since it is false, the statement under if block is omitted. Control passes to else statement and condition (age == 17) is tested, since it is true, statements under else if block is executed. The return statement under the else if block passes the control back to calling function i.e main(). If the return statement would not have been there in the else if block, the control would have been moved ahead to execute the statement following if-else statement.

The second form of the return statement is used to return values from a function. The expression following return can be any constant, variable, function call etc.

The following program computes the factorial of a number using a function.

Expected Output:

How it works

There is nothing extraordinary happening in the main() function, so we are only going to explain how factorial() function works. Let’s say the user entered 5, in line 18 the factorial() function is called, along with an argument of type int. In line 30, if condition (n==0) is checked, since it is false, statements inside the if block are omitted. If the condition (n==0) would have been true, then the statement return 1; (Why we are returning 1, because factorial of 0 is 1 ) would have been executed, and the control would have been transferred back to main() function, effectively terminating the factorial() function.

In line 34, two variables f and i of type int are declared and the variable f is assigned a value of 1.

In line 36, for loop’s initialization expression is executed, i is assigned the value of n. Then the condition (i>0) is tested, since it is true, statement inside the for body is executed. At this point, the value of f is 5. This completes the first iteration. Then update expression i-- is executed. This process continues until i is greater than 0. The following table shows the value of i and fact after each iteration.

Iteration Value of i Value of fact
1 5 5
2 4 5*4 = 20
3 3 20* 3 = 60

When i reaches 0, the condition (i > 0) becomes false and the control breaks out of the for loop to execute statements following the loop.

In line 40, the return statement causes the factorial() function to terminate and return the value of variable f to the calling function main().

3 thoughts on “The return statement in C

  1. Please correct the programme because first you have written eligible and at the function call you have written eligible_or_not this will give error.We have to use eligible every where for calling the function.

Leave a Comment