Home > Net >  Position of the return statement mattering in C? (variables becoming 0)
Position of the return statement mattering in C? (variables becoming 0)

Time:04-20

I'm new to C, so can someone please explain to me why this returns the expected output

int f_to_i(float x) {
        int inc = 0;
        if (x < 0) {
            int inc = 0;
            while (inc > x) {
                inc -= 1;
                return inc;
            }
        }
        else {
            int inc = 0;
            while (inc < x) {
                inc  = 1;
                return inc;
            }
        }
    }

but this returns 0 no matter what?

int f_to_i(float x) {
        int inc = 0;
        if (x < 0) {
            int inc = 0;
            while (inc > x) {
                inc -= 1;
            }
        }
        else {
            int inc = 0;
            while (inc < x) {
                inc  = 1;
            }
        }
        return inc;
    }

I'm just confused because the only thing that changes is the position of the return statement. I just started learning C from Ruby, so I'm not used to a lot of these weird features. If anybody can help me and tell me why this is happening that'd be great.

CodePudding user response:

Each time you have int inc it declares a new variable in a different scope. So the code actually has three inc variables and the return in each case is returning a different variable.

Here is the code annotated with my comments. Each of the variable scopes is numbered.

int f_to_i(float x) {
    int inc = 0; // <--- (1)
    if (x < 0) {
        int inc = 0; // <--- (2)
        while (inc > x) {
            inc -= 1;
            return inc;
        }
    }
    else {
        int inc = 0; // <--- (3)
        while (inc < x) {
            inc  = 1;
            return inc;
        }
    }
}

In that first example code it is the variable at scope (2) or scope (3) that is returned. In the second example code it is the variable at scope (1) that is returned.

CodePudding user response:

So in the fist code stub you have returned the inc value inside the conditional statement. In simple words, it will go inside the if block(A) if x<0 and execute only that code block(A) and come to the end of code block (C) based on the given float value. As a result, the return statement within the A block with inc = -1 will be executed. And if x>0, the if condition becomes false, and it enters the else block(B) and executes only that code block before exiting the B code block and reaching the end of code block C. As a result, the return statement within the B block with inc = 1 will be executed.

int f_to_i(float x) {
        int inc = 0;
        if (x < 0) {
            int inc = 0;
            while (inc > x) {
                inc -= 1;
                return inc;
            }
        }// (A)
        else {
            int inc = 0;
            while (inc < x) {
                inc  = 1;
                return inc;
            }
        }// (B)
    }// (C)

However, in the second code, you've placed the return statement outside of each conditional statement. Simply stated, you've placed the return statement in the local scope or block scope. Because you defined inc = 0 in the block scope, it returns inc as 0 rather than the value specified by the conditional statements. Because the value inc = 1 or inc = -1 will only be applied within the if (A) or else (B) code blocks. After you exit that specific code block inc value is reset to the default value provided by the block scope.

int f_to_i(float x) {
        int inc = 0;
        if (x < 0) {
            int inc = 0;
            while (inc > x) {
                inc -= 1;
            }
        }/End of the code block A
        else {
            int inc = 0;
            while (inc < x) {
                inc  = 1;
            }
        }//End of the code block B
        return inc;
    }

I hope this may solve your confusion.If you need more clarification, feel free to ask them.

  •  Tags:  
  • c
  • Related