Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return in a for loop C

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!!:)

like image 832
tvp458 Avatar asked Sep 07 '25 03:09

tvp458


1 Answers

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

like image 59
CIsForCookies Avatar answered Sep 10 '25 02:09

CIsForCookies