3

New! Save questions or answers and organize your favorite content.
Learn more.

In the following code, would anything be returned?

                #include <stdio.h>  int try_this (int in);  int main (void) {     try_this (5); }  int try_this (int in) {     int i = 1;      for (i = 0; i < in; i = i + 2) {         return i;     }      return i; }                              

Since there's a return in the for loop, would the code just return nothing since there's nothing after the function is called? Or would i be returned as a number, like 1(because of the declaration in try_this) or 6(because of the loop)? Thank you!!:)

David C. Rankin's user avatar

asked May 14, 2017 at 9:42

tvp458's user avatar

5

  • Umm... The function is named try_this. I think you should just do that.

    May 14, 2017 at 9:45

  • Every time 0 is returned

    May 14, 2017 at 9:45

  • As soon as you reach your first return the function returns the value specified and the function stack is destroyed (released back to the system). ...And your compiler should be screaming at you that function main is type int and therefore should return a value (e.g. return try_this(5);). Always compile with at minimum -Wall -Wextra in your compile string and do not accept code until it compiles without any warnings.

    May 14, 2017 at 9:47

  • It will return 0, but you are not using the returned value in main.

    May 14, 2017 at 9:48

  • The initial condition of the for loop sets i to be 0. The first iteration, with i equal to 0, will hit return i. So the function will always return 0.

    May 14, 2017 at 9:49

5 Answers 5

When the first return statement is encountered, the function will return. It's easy to see that the function will start the loop and while i=0 it will return i. So each time you call try_this you'll get 0

Also, return 0; from main...

David C. Rankin's user avatar

answered May 14, 2017 at 9:49

CIsForCookies's user avatar

3

  • You can wrap inline code in your paragraph above in backticks (e.g. '`') to format as code. It helps with the readability of your answer and shows a bit of additional effort.

    May 14, 2017 at 9:53

  • No big deal, I fixed it for you. It must be a bear to format on those darn mobile keyboards...

    May 14, 2017 at 10:39

  • BTW: If control reaches the end of the main function, return 0; is executed.

    Aug 4 at 20:44

When you enter the for loop for the first time, i is 0 and less than the 5. Then it execute the statement return i; in for loop, which will finish executing the try_this() funciton.

answered May 14, 2017 at 9:54

LF00's user avatar

0

What you are basically asking is that if the function will return something even though there is no code after the function call.

The answer is yes, it will return. It doesn't matter if there is code later or not. The function will return (in this case 0) It's just that there's nobody to catch it.

answered May 14, 2017 at 10:00

Parth K's user avatar

The function try_this() always returns 0:

  • i is initially set to 1
  • then i is set to 0 in the initial clause of the for statement
  • if i < in, the loop body is executed and the value of i is returned, namely 0.
  • otherwise the loop is skipped and the next statement is return i; so 0 is returned too.

Regarding the main function: it ignores the return value of try_this(5) and reaches the end of its body without a return statement. This is bad style and would trigger undefined behavior for any other function, but since C99 the behavior is defined as a special case for the main function and the value 0 is returned to the startup code, which exits back to the system with a termination status of 0, which means successful execution.

answered Aug 4 at 20:49

chqrlie's user avatar

if you want to store the value of 'I' i would recommend you to use "while loop"

                  int i = 0; while(i < in) {  i++; }                                  

answered Aug 4 at 20:22

Mohamed Ezat's user avatar

1

  • Um, no? for is still the better option here.

    Aug 8 at 17:35