It is currently Thu Sep 19, 2019 11:46 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Global variables inside Procedures
PostPosted: Fri Aug 23, 2019 9:45 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon May 09, 2011 9:36 am
Posts: 461
is it OK to place Global variables inside Procedures ?

some of these same Global variables would then be accessed in other Procedures in my Program .

anyone see potential problems with doing this ?

_________________
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison


Top
 Profile  
Reply with quote  
 Post subject: Re: Global variables inside Procedures
PostPosted: Fri Aug 23, 2019 10:09 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3055
Location: Boston, MA
Why not?
I put many def's in Main() or Init().

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


Top
 Profile  
Reply with quote  
 Post subject: Re: Global variables inside Procedures
PostPosted: Fri Aug 23, 2019 10:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Sep 22, 2010 1:17 pm
Posts: 297
Location: United Kingdom
You should be aware that there is the potential to create accidentally a scope ambiguity bug:-

Code:
Procedure P1()
 
  V = 1
 
  Debug V
 
EndProcedure

Procedure P2()
 
  Global V.I
 
  Debug V
 
EndProcedure

P1()
P2()

Debug V


If you run this code you will get the output:-
Code:
1
0
0

The variable V isn't truly global despite appearing to be. P1 will implicitly create a local variable, which will be inaccessible to other procedures.

(Putting EnableExplict at the top of your code will detect this error, but if your code isn't currently written to allow that you'd need a refactor).


Top
 Profile  
Reply with quote  
 Post subject: Re: Global variables inside Procedures
PostPosted: Fri Aug 23, 2019 10:54 pm 
Online
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3654
Location: Berlin, Germany
viewtopic.php?f=13&t=54221

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Global variables inside Procedures
PostPosted: Sat Aug 24, 2019 1:27 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon May 09, 2011 9:36 am
Posts: 461
Little John wrote:
https://www.purebasic.fr/english/viewtopic.php?f=13&t=54221


from Fred :
Quote:
Global is OK in procedures


i'm doing something like this ... i call the 1st Procedure which holds the Global variables
then call the other Procedures that calculate data

Code:
EnableExplicit

Procedure SetGlobalVar()
  Global var=1
EndProcedure

Procedure ShowGlobalVar()
  Debug var
EndProcedure


SetGlobalVar()
ShowGlobalVar()

_________________
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye