|
Post by bitvast on Sept 13, 2014 11:36:45 GMT 1
It says in the manual: So I would expect x to be 10 at the end of this program, but it isn't:
FUNCTION f() x = 0 INCR x, 5 RETURN x END FUNCTION
'this statement has no effect INCR x, 5
PRINT f() PRINT x 5 5
|
|
|
Post by Pjot on Sept 13, 2014 14:53:51 GMT 1
Hi bitvast,
No, it isn't. Because when you call the function f() after the INCR, the value of 'x' is reset back to 0 in the function f().
BR Peter
|
|
|
Post by bitvast on Sept 13, 2014 15:17:23 GMT 1
Thanks Peter, no bug then. It seems that BaCon, unlike some languages, doesn't allow two variables to have the same name, even when one is LOCAL to a sub/function and the other GLOBAL. But I guess that's probably a good thing.
|
|
|
Post by Pjot on Sept 13, 2014 17:03:37 GMT 1
Well, it works like this: - If a variable explicitly is declared as a global variable (using DECLARE) then such a variable is visible in functions and subs too. This means, that a variable with such name cannot also exist with a local scope. - Similar for implicit declared variables, which always have a global scope. In your demo program, the 'INCR x, 5' in the main section implicitly declares the variable 'x' globally. However, if you explicitly declare a variable as LOCAL in each sub/function then this is not problem: FUNCTION f() LOCAL x INCR x, 5 RETURN x END FUNCTION
FUNCTION g() LOCAL x INCR x, 6 RETURN x END FUNCTION
PRINT f() PRINT g()
HTH Peter
|
|
|
Post by bitvast on Sept 13, 2014 17:32:51 GMT 1
Peter,
Thanks for clearing that up. I was writing a demo program for the tutorial, but kept getting errors.
|
|