Write a Return Statement That Returns the Variable Intcricketscore.

  1. Home
  2. C Programming Tutorial
  3. The return statement in C

The return statement in C

Last updated on July 27, 2020


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.

                      1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
                                            #include                      <stdio.h>                                            void                      eligible_or_not                      (                      int                      x                      );                      int                      main                      ()                      {                      int                      n                      ;                      printf                      (                      "Enter age: "                      );                      scanf                      (                      "%d"                      ,                      &                      n                      );                      eligible_or_not                      (                      n                      );                      // signal to operating system everything works fine                      return                      0                      ;                      }                      void                      eligible_or_not                      (                      int                      age                      )                      {                      if                      (                      age                      >=                      18                      )                      {                      printf                      (                      "You are eligible to vote"                      );                      return                      ;                      }                      else                      if                      (                      age                      ==                      17                      )                      {                      printf                      (                      "Try next year"                      );                      return                      ;                      }                      else                      {                      printf                      (                      "You are not yet ready"                      );                      return                      ;                      }                      }                    

Expected Output:

1st run:

                      Enter age: 16 You are not yet ready                    

2nd run:

                      Enter age: 17 Try next year                    

3rd run:

                      Enter age: 19 You are eligible to vote                    

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.

                      1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
                                            #include                      <stdio.h>                                            int                      factorial                      (                      int                      x                      );                      // declaration of factorial function                      int                      main                      ()                      {                      int                      n                      ;                      printf                      (                      "Calculate factorial:                                            \n\n                      "                      );                      printf                      (                      "Enter number : "                      );                      scanf                      (                      "%d"                      ,                      &                      n                      );                      if                      (                      n                      <                      0                      )                      {                      printf                      (                      "                      \n                      Factorial is only defined for positive numbers"                      );                      }                      else                      {                      printf                      (                      "                      \n                      %d! = %d"                      ,                      n                      ,                      factorial                      (                      n                      ));                      // calling factorial function                      }                      // signal to operating system everything works fine                      return                      0                      ;                      }                      // definition of factorial function                      int                      factorial                      (                      int                      n                      )                      {                      if                      (                      n                      ==                      0                      )                      {                      return                      1                      ;                      }                      int                      f                      =                      1                      ,                      i                      ;                      for                      (                      i                      =                      n                      ;                      i                      >                      0                      ;                      i                      --                      )                      {                      f                      =                      f                      *                      i                      ;                      }                      return                      f                      ;                      }                    

Expected Output:

                      Calculate factorial:  Enter number : 5  5! = 120                    

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



Ezoic

Write a Return Statement That Returns the Variable Intcricketscore.

Source: https://overiq.com/c-programming-101/the-return-statement-in-c/

0 Response to "Write a Return Statement That Returns the Variable Intcricketscore."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel