We need Define to have local variables outside the scope of procedures.Foz wrote:Ok, on a very similar theme then:
if we have the Global keyword for global variables, and the Protected keyword for local variables, why should we use Define at all?
Or to put it another way, what is the difference between Define and Global?
Define vs Protected in procedures
Re: Define vs Protected in procedures
Re: Define vs Protected in procedures
... ok, I must be missing the point.
In my mind, outside of a procedure is "global" territory, inside a procedure is "local" territory.
Is there some middle ground that is between global and local that I'm not aware of?
In my mind, outside of a procedure is "global" territory, inside a procedure is "local" territory.
Is there some middle ground that is between global and local that I'm not aware of?
-
MachineCode
- Addict

- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: Define vs Protected in procedures
No, a global variable can be used anywhere, both inside and outside of a procedure. But a variable used outside a procedure, that hasn't been set as global, is only valid outside of procedures.Foz wrote:In my mind, outside of a procedure is "global" territory
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
Re: Define vs Protected in procedures
Hi Foz,
I agree with you, I rarely use Define...
But, if you did, then the procedures do not see the variable unless Shared is used.
I agree with you, I rarely use Define...
But, if you did, then the procedures do not see the variable unless Shared is used.
Code: Select all
;Constants and Structures...
Procedure init()
Debug i ;<--- 0
EndProcedure
Procedure Main()
Shared i
Debug i ;<--- 10
EndProcedure
Define.i i=10
Init()
Main()
End
;DataSections...
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Define vs Protected in procedures
I've just realised what you were talking about - running code outside of Procedures, vs code inside Procedures.
For small, quick and dirty apps, it's invaluable, for anything that involves a Procedure, I move everything into Procedures so it keeps code clean, so I've never needed a local variable outside of a procedure , and I can't say I'm going to start either...
*** edit: Thank you skywalk - I didn't know about the Shared keyword, so that does make sense. It's ugly, but that's my way of thinking!
For small, quick and dirty apps, it's invaluable, for anything that involves a Procedure, I move everything into Procedures so it keeps code clean, so I've never needed a local variable outside of a procedure , and I can't say I'm going to start either...
*** edit: Thank you skywalk - I didn't know about the Shared keyword, so that does make sense. It's ugly, but that's my way of thinking!
Last edited by Foz on Tue Mar 08, 2011 10:01 pm, edited 1 time in total.
Re: Define vs Protected in procedures
Better:
Define the variable before share it!
init shows the error.
Code: Select all
EnableExplicit
Define.i i=10
;Constants and Structures...
Procedure init()
Debug i ;<--- 0
EndProcedure
Procedure Main()
Shared i
Debug i ;<--- 10
EndProcedure
Init()
Main()
End
;DataSections...
init shows the error.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Re: Define vs Protected in procedures
There's nothing wrong with putting structures inside procedures, especially if the code inside the procedure uses it. Keeps the procedure self-contained. It shouldn't raise an error or be banned.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.


