Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#18 Functions don't understand global variables

closed-duplicate
nobody
General (151)
5
2001-05-02
2001-01-17
Allan Crooks
No

Start up the Interpreter, type in i=0; and add the following function:

increment () {i++;}

No matter how many times increment is invoked, it doesn't alter the global value of i. If you try changing the function to j, it will complain about j not existing.

Discussion

  • Dror Matalon
    Dror Matalon
    2001-01-26

    This doesn't work either.
    ii = 0;
    for (ii=0 ; ii < 10; ii++)
    {
    print (ii);
    }
    print ("after the loop:" + ii);

    The result is 0 rather than 10.

     
  • Pat Niemeyer
    Pat Niemeyer
    2001-05-02

    Logged In: YES
    user_id=18885

    This isn't actually a bug, but is a confusing aspect of the
    way bsh deals with variables. All variable assignment is
    local unless you scope it. Bsh is simply incrementing the
    value in the local scope.

    I will add better documentation of this confusing aspect of
    bsh. If developers want to talk about this (after perhaps
    reading the email discussions or write-up) please let me
    know. I'm open to suggestions... but there are reasons
    why this is so. --Pat

    The follow up looks like bug #418202 or related (for scope
    issue).

     
  • Pat Niemeyer
    Pat Niemeyer
    2001-05-02

    • status: open --> closed-duplicate