PureBasic Forum
https://www.purebasic.fr/english/

Global variables inside Procedures
https://www.purebasic.fr/english/viewtopic.php?f=13&t=73464
Page 1 of 1

Author:  VB6_to_PBx [ Fri Aug 23, 2019 9:45 pm ]
Post subject:  Global variables inside Procedures

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 ?

Author:  skywalk [ Fri Aug 23, 2019 10:09 pm ]
Post subject:  Re: Global variables inside Procedures

Why not?
I put many def's in Main() or Init().

Author:  spikey [ Fri Aug 23, 2019 10:27 pm ]
Post subject:  Re: Global variables inside Procedures

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).

Author:  Little John [ Fri Aug 23, 2019 10:54 pm ]
Post subject:  Re: Global variables inside Procedures

viewtopic.php?f=13&t=54221

Author:  VB6_to_PBx [ Sat Aug 24, 2019 1:27 am ]
Post subject:  Re: Global variables inside Procedures

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()

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/