Page 1 of 1

GLOBAL and SHARED variables

Posted: Fri Jan 10, 2025 2:22 pm
by ervin
Hi everyone.

I am trying to write a routine to print large sprites using small tile pieces.
Anyway, I am having some problems with GLOBAL and SHARED variables.

I've reduced the code to a very basic example.

Code: Select all

OPTION EXPLICIT ON

DIM a AS BYTE

PROCEDURE testProc
	SHARED a
	PRINT a
END PROCEDURE

a=8
CALL testProc
On the CPC target, this prints 8, which is the expected result.

However, this code:

Code: Select all

OPTION EXPLICIT ON

GLOBAL a AS BYTE

PROCEDURE testProc
	PRINT a
END PROCEDURE

a=8
CALL testProc
Causes a compilation error:

undefined variable (OPTION EXPLICIT ON) (a)

If I set OPTION EXPLICIT OFF, the program compiles, and works as expected.
But that removes the desired benefits of using OPTION EXPLICIT.

Does GLOBAL only work when OPTION EXPLICIT is OFF?

Re: GLOBAL and SHARED variables

Posted: Fri Jan 10, 2025 2:32 pm
by ervin
I solved it!

Code: Select all

OPTION EXPLICIT ON

DIM a AS BYTE
GLOBAL a

PROCEDURE testProc
	PRINT a
END PROCEDURE

a=8
CALL testProc

Re: GLOBAL and SHARED variables

Posted: Sat Jan 11, 2025 7:02 am
by spotlessmind1975
Hi ervin, and welcome! :D
ervin wrote: Fri Jan 10, 2025 2:22 pm undefined variable (OPTION EXPLICIT ON) (a)
This error should be correct, since the GLOBAL command allows you to tell ugBASIC to consider a variable as "global" but the instruction neither initializes the variable nor creates it. This means that there is no "promise" on the type. In this topic, I asked if this behaviour is the expected one. I couldn't come up with an answer. In the meantime this semantics spread, so in the end I let the behavior remain as it is.

Re: GLOBAL and SHARED variables

Posted: Sun Jan 12, 2025 9:03 am
by ervin
Makes sense, thanks!
(by the way, I'm "poppichicken" that has been reporting bugs on github).
:)