GLOBAL and SHARED variables

This forum is dedicated to those who want support programming in ugBASIC.
Questo forum è dedicato a chi desidera supporto per programmare in ugBASIC.
Ce forum est dédié à ceux qui souhaitent prendre en charge la programmation en ugBASIC.
Post Reply
ervin
Posts: 3
Joined: Wed Jul 03, 2024 12:41 pm

GLOBAL and SHARED variables

Post 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?
ervin
Posts: 3
Joined: Wed Jul 03, 2024 12:41 pm

Re: GLOBAL and SHARED variables

Post 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
spotlessmind1975
Site Admin
Posts: 178
Joined: Fri Oct 06, 2023 8:25 pm

Re: GLOBAL and SHARED variables

Post 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.
ervin
Posts: 3
Joined: Wed Jul 03, 2024 12:41 pm

Re: GLOBAL and SHARED variables

Post by ervin »

Makes sense, thanks!
(by the way, I'm "poppichicken" that has been reporting bugs on github).
:)
Post Reply