-1

I decided to change the scope of a function operator from global to local. After changing from the commented code, I found that my code no longer runs and exits with error:

C4700 uninitialized local variable 'n' used.\

This seems to be a quite obvious contradiction to the actual method of local resolution. Does anyone have an explanation for this?

int Combs::factorial(int a)
{
    //value = 1;
    int n;
    for (int i = a; i >0; i--)
    {
        n *= i;
    }
    cout << n;
    return n;
}

1 Answers1

2

When the variable is declared at global/file scope, the compiler initializes it for you, when it's local to a function, it doesn't, so you need to do it yourself.

n is indeed used unitialized, when it was a global variable it was not.

anastaciu
  • 23,467
  • 7
  • 28
  • 53