Page 2 of 2
Re: Define vs Protected in procedures
Posted: Tue Mar 08, 2011 7:28 pm
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.
Re: Define vs Protected in procedures
Posted: Tue Mar 08, 2011 9:46 pm
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?
Re: Define vs Protected in procedures
Posted: Tue Mar 08, 2011 9:51 pm
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.
Re: Define vs Protected in procedures
Posted: Tue Mar 08, 2011 9:54 pm
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...
Re: Define vs Protected in procedures
Posted: Tue Mar 08, 2011 9:57 pm
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!
Re: Define vs Protected in procedures
Posted: Tue Mar 08, 2011 9:59 pm
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.
Re: Define vs Protected in procedures
Posted: Wed Mar 09, 2011 12:29 am
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.