C if You Return in a Loop Will Loop Continue
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!!:)
asked May 14, 2017 at 9:42
tvp458tvp458
39 1 gold badge 1 silver badge 2 bronze badges
5
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...
answered May 14, 2017 at 9:49
CIsForCookiesCIsForCookies
11.5k 8 gold badges 52 silver badges 108 bronze badges
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
LF00LF00
25.5k 27 gold badges 142 silver badges 271 bronze badges
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 KParth K
547 3 silver badges 17 bronze badges
The function try_this()
always returns 0
:
-
i
is initially set to1
- then
i
is set to0
in the initial clause of thefor
statement - if
i < in
, the loop body is executed and the value ofi
is returned, namely0
. - otherwise the loop is skipped and the next statement is
return i;
so0
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
chqrliechqrlie
120k 10 gold badges 111 silver badges 176 bronze badges
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
1
-
Um, no?
for
is still the better option here.Aug 8 at 17:35
Source: https://stackoverflow.com/questions/43962487/return-in-a-for-loop-c
Umm... The function is named
try_this
. I think you should just do that.May 14, 2017 at 9:45
Every time
0
is returnedMay 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 thatfunction 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 setsi
to be0
. The first iteration, withi
equal to0
, will hitreturn i
. So the function will always return0
.May 14, 2017 at 9:49