0

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;
}
Chris Hoy
  • 1
  • 1

1 Answers1

1

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
  • 22,293
  • 7
  • 26
  • 44