Is there a function that is the inverse of Define, Global, etc. ? (was: small and quick question)

Just starting out? Need help? Post your questions and find answers here.
ZX80
Enthusiast
Enthusiast
Posts: 375
Joined: Mon Dec 12, 2016 1:37 pm

Is there a function that is the inverse of Define, Global, etc. ? (was: small and quick question)

Post by ZX80 »

Hi, all.

We have Define to declare variable etc. and Defined for check / determine / found it. My question is this:

Is there a function that is the inverse of Define, Global, etc. ?
In other words, I want that the program, at a certain stage, forgot about the existence of a previously declared variable. Then global variables won't seem so scary, right ? :mrgreen:
Has anyone thought about it or I just don't know about it ?

Okay. Here's a micro-example:

Code: Select all

EnableExplicit

Global *mem = AllocateMemory(100)

Procedure SomeProc()
  ; do something

  If Defined(*mem, #PB_Variable)
    Debug "Okay. I see that a variable named '*mem' already exists."
    Debug "Now I want that the program to forgot about this variable, but how ?"
    Debug "Of course, the previously requested memory must be freed first."
    FreeMemory(*mem)
    Debug ""
    Debug "Probably also need to clear the pointer itself"
    *mem = #Null
  EndIf
EndProcedure

SomeProc()
If Defined(*mem, #PB_Variable)
  Debug "As you can see, the variable still exists / is recognized by the program   :("
EndIf
Thus, a global variable exists only in a separate section of the program.
Please, let's not argue whether this is correct. I'm interested in this possibility itself.
Thank you in advance.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Is there a function that is the inverse of Define, Global, etc. ? (was: small and quick question)

Post by wilbert »

ZX80 wrote: Sat Aug 03, 2024 3:12 pmIs there a function that is the inverse of Define, Global, etc. ?
No there isn't.
I don't know what you need it for but maybe you can use Module.
Windows (x64)
Raspberry Pi OS (Arm64)
ZX80
Enthusiast
Enthusiast
Posts: 375
Joined: Mon Dec 12, 2016 1:37 pm

Re: Is there a function that is the inverse of Define, Global, etc. ? (was: small and quick question)

Post by ZX80 »

wilbert,

No doubt, you are right ! Modules are also knowns as namespaces. I was wondering if there was such an opportunity and how much demand it had.
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Is there a function that is the inverse of Define, Global, etc. ? (was: small and quick question)

Post by AZJIO »

And we are not afraid of global variables. Surely by adding “g_” to the beginning of such a variable, it will not collide with variables in other functions. In addition, you can search for "g_" to be sure that you do not have a single match. When searching, use match from start.
Today I was just thinking, why do I need to get away with global variables? There is no reason for this, and what's more, you will have to create extra temporary variables inside functions. For example, I store temporary results in a tmp or tmp$ variable and every time I reason, why should I waste memory if I can use one global variable instead of 10 variables inside a function. Although the loss is only 4*10=40 bytes.
I have never used "Shared", perhaps this is the case when the variable is defined as "Define", but will be visible to the function if necessary.

Here we forgot about the variable

Code: Select all

Procedure SomeProc()
	Protected *mem
The Defined function is a compiler function and will be defined not while the program is running, but during compilation, and in the program it will already be either 0 or 1
ZX80
Enthusiast
Enthusiast
Posts: 375
Joined: Mon Dec 12, 2016 1:37 pm

Re: Is there a function that is the inverse of Define, Global, etc. ? (was: small and quick question)

Post by ZX80 »

AZJIO,

Does it follow from your words that this is impossible ? What I asked at the beginning of this topic. I'm sure that's the case.
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Is there a function that is the inverse of Define, Global, etc. ? (was: small and quick question)

Post by AZJIO »

1. You don't have to delete the variable, you don't have to use it.
2. You can create a structure with a field, and then delete the structure. But it is easier not to use a variable than to make it impossible to use. There's only one byte.
User avatar
Bisonte
Addict
Addict
Posts: 1320
Joined: Tue Oct 09, 2007 2:15 am

Re: Is there a function that is the inverse of Define, Global, etc. ? (was: small and quick question)

Post by Bisonte »

If you are using "Protected" it doesn't care....

Code: Select all

Global variable1 = 123

Procedure.i Change()
  
  Protected variable1 = 100
  
  Debug variable1
  
EndProcedure

Debug variable1
Change()
Debug variable1
this is not changing the global variable ....
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Post Reply