-2

Why can't we just decide to have it non-static? What is it letting us do?

static int nLen = 0;
    if (nLen)
    {
        nLen--;
        if (rand() % 4 == 0)
        {
            float light = (float)rand()/(float)RAND_MAX;
            if (rand() % 2 == 0)
            {
                Program.SendUniform("lightEmissive.color", 0.0, 0.0, 0.0);
                Program.SendUniform("lightPoint.diffuse", 0.0, 0.0, 0.0);
            }
            else
            {
                Program.SendUniform("lightEmissive.color", 1.0 * light, 1.0 * light, 0.8 * light);
                Program.SendUniform("lightPoint.diffuse", 0.15 * light, 0.15 * light, 0.15 * light);
            }
        }
    }
    else
    {
        Program.SendUniform("lightEmissive.color", 1.0, 1.0, 0.8);
        Program.SendUniform("lightPoint.diffuse", 0.15, 0.15, 0.15);
        if (rand() % 300 == 0)
            nLen = rand() % 180;
    }
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
GreenBandit
  • 143
  • 1
  • 1
  • 4
  • Who are "we"? I don't think I should have a say in what you make `static` or not. What is the intent of the code? – John Sensebe Jan 25 '16 at 18:14
  • Is this code part of a class member function? In that case, `nLen` should probably be a private member variable and initialized as such. – John Sensebe Jan 25 '16 at 18:18

3 Answers3

2

When static is used as a qualifier within a function, it means that the variable will remain allocated and retain its value from one function call to the next. In other words it has global lifetime. (It is still only initialized the first time the function is called).

When static is used as a qualifier to a global variable, it means that the variable remains internal to the current compilation unit (source file). This variable cannot be accessed by name from another compilation unit using extern type name (although it can be accessed via a pointer). if two compilation units use the same static global variable, the names will also not clash and the program will compile.

doron
  • 27,972
  • 12
  • 65
  • 103
0

It is intended to keep the variable only visible to the source file. For example, you won't be able to access it in another source file. If there is no requirement of any sort...you are free to make it non-static.

SarvanZ
  • 90
  • 4
0

nLen is static to skip first run of the "if" block. Local nLen would make "if" block run every time the method is called.

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20