Define vs Protected in procedures

Just starting out? Need help? Post your questions and find answers here.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: Define vs Protected in procedures

Post by eesau »

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?
We need Define to have local variables outside the scope of procedures.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Define vs Protected in procedures

Post by Foz »

... 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?
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Define vs Protected in procedures

Post by MachineCode »

Foz wrote:In my mind, outside of a procedure is "global" territory
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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
skywalk
Addict
Addict
Posts: 4260
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Define vs Protected in procedures

Post by skywalk »

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.

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
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Define vs Protected in procedures

Post by Foz »

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!
Last edited by Foz on Tue Mar 08, 2011 10:01 pm, edited 1 time in total.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Define vs Protected in procedures

Post by ts-soft »

Better:

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...
Define the variable before share it!
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.
Image
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Define vs Protected in procedures

Post by PB »

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.
Post Reply