-1

I am a newborn programmer and i've been studying trace tables thus far. However, it seems i'm unable to understand this one. By what i could gather the function would return (-15) as the variable 'a' receives 25 and later the 'num', which is 10, is subtracted by the former.

Yet it seems i'm wrong by PythonTutor's calculations and i still cannot get why num is still 10 after all that C magic. Would someone be so kind to explain why is this happening?

#include <stdio.h>

int num;
int func(int a, int b);

int main() {
    int first = 0, sec = 50, num2;
    num = 10;
    num += func(first, sec);
    printf("\nnum = %d\tfirst = %d\tsec = %d", num, first, sec);

    return 0;
}

int func(int a, int b) {
    a = (a + b) / 2;
    num -= a;
    return a;
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • 2
    It seems like you subtract 25 in the `func` and add it again after the function call finishes. – Philipp Jun 03 '22 at 19:16
  • I suggest you put some printfs at strategic point of this code so you can watch how the variables evolve during the execution of this code. Or better: invest 15 minutes to learn the basics of your debugger (yes, I'm sure you have one). – Jabberwocky Jun 03 '22 at 19:35
  • And you are wrong in the point where you assume the function returns `-15`. That would be `num` but the value returned is `a` and this is `25` – Philipp Jun 03 '22 at 19:41
  • 1
    After examining @chux answer, it would be wise to use a debugger to step through and recreate his results. That is a skill for any new programmer that is just as important as mastering the syntax and language. See [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), consider a chat with the duck, and see [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/3422102) The key is you want to examine the value of `num` at each point within your program to determine where things go south. – David C. Rankin Jun 03 '22 at 22:14
  • [You don't need to tell us that you are new to `X`. It is just noise here](https://meta.stackoverflow.com/q/296391/11107541). – starball Dec 18 '22 at 23:39

1 Answers1

2

Would someone be so kind to explain why is this happening?

Let us take this step by step

int num;
int func(int a, int b);

int main() {
    // 1) `num == 0` as it is a global variable without explicit initialization.

    int first = 0, sec = 50, num2;

    // 2) `num` is 10
    num = 10;

    // 3) Before the function call, `num` is 10
    num += func(first, sec);
    // 7) At the end of the function call, `num` is -15 and 
    // the return value of 25 is added to it due to +=.
    // 8) `num` is now 10 again.

    printf("\nnum = %d\tfirst = %d\tsec = %d", num, first, sec);
    return 0;
}

int func(int a, int b) {
    a = (a + b) / 2;
    // 4) `a` is 25, `num` is 10

    // 5) `num` becomes 10 - 25 --> -15
    num -= a;

    // 6) `a == 25` and 25 is returned
    return a;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256