#18 Functions don't understand global variables

General (151)

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.


  • 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

    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

  • Pat Niemeyer

    Pat Niemeyer - 2001-05-02
    • status: open --> closed-duplicate

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks