[PB5.70] Possible bug calling procedures and variables?

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

[PB5.70] Possible bug calling procedures and variables?

Post by marcoagpinto »

Hello!

Is this a bug?

Code: Select all

Declare test_it(variable)

Global variable=#False


Debug "value of variable:"+Str(variable)
test_it(#False)
Debug "value of variable:"+Str(variable)


End


Procedure test_it(variable)
  variable=#True
  ProcedureReturn #False
EndProcedure

It returns #False although inside the procedure I change the variable to #True.


EDIT: changed the version on topic, I only tested with 5.70 not 5.71, sorry.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [PB5.70] Possible bug calling procedures and variables?

Post by skywalk »

Do not post to Bugs if you are not sure. :evil:
Your code is wrong.
You pass in a Global variable which is copied inside the Procedure.
This is called ByVal.
What happens to the variable inside the Procedure is like Vegas. It stays there.
After the Procedure, the Global variable is unchanged.

Code: Select all

EnableExplicit
Procedure test_ByVal(variable)
  ; variable is passed by value.
  ; Even though it matches name of a Global variable
  ; it does not change the Global value.
  variable=1
  ProcedureReturn 0
EndProcedure
Procedure test_ByRef(*variable.Integer)
  ; variable is passed by reference.
  ; This changes the Global value.
  *variable\i=1
  ProcedureReturn 0
EndProcedure
Global variable=0
Debug "BEFORE TEST variable = "+Str(variable)
test_ByVal(variable)
Debug "AFTER ByVal variable = "+Str(variable)
test_ByRef(@variable)
Debug "AFTER ByRef variable = "+Str(variable)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: [PB5.70] Possible bug calling procedures and variables?

Post by Demivec »

Code: Select all

;// Precedence for scope of variables (and not prototypes, structures, enumerations, procedures, etc.)  //

;The first declared prohibits further declaration of all others having the same name that are active in the current scope.

;--- Outside procedure ---

     Global      (global scope)
     Threaded    (global scope)
     Define      (local scope to main code outside procedures)


;--- Inside procedure ---

     parameter  (local scope to procedure)
     Global     (global scope)
     Threaded   (global scope)
     
     Protected  (local scope, can be defined after a Global and will override Global's scope)
     Static     (local scope)
     Define     (local scope, can also repeat the declaration of a parameter, i.e. they are equivalent except Define can also assign an initial value)
Last edited by Demivec on Tue Jun 18, 2019 7:06 am, edited 1 time in total.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [PB5.70] Possible bug calling procedures and variables?

Post by Little John »

It would be safest if PureBasic did not allow a global variable to have the same name as a local variable or parameter.

@marcoagpinto:
viewtopic.php?f=4&t=4701
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: [PB5.70] Possible bug calling procedures and variables?

Post by Josh »

@marcoagpinto
A parameter is like a variable defined with 'Protected' inside the procedure

@Little John
For variables, it is not allowed unless you are using 'Protected'.
For parameters as written above, they are like 'Protected'.

@Any Mod
Please move to section 'Questions'
sorry for my bad english
acreis
Enthusiast
Enthusiast
Posts: 182
Joined: Fri Jun 01, 2012 12:20 am

Re: [PB5.70] Possible bug calling procedures and variables?

Post by acreis »

Global variables considered harmful. Avoid at any cost. When impossible give it a very weird name, as "___glb_my_inevitable_global_variable_". I have only one project in purebasil some 100k lines and less than 20 global variables, and counting down.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [PB5.70] Possible bug calling procedures and variables?

Post by Little John »

Josh wrote:@Little John
For variables, it is not allowed unless you are using 'Protected'.
For parameters as written above, they are like 'Protected'.
I don't see what this comment has got to do with my above message.
The meaning of that message is "... if PureBasic did never allow ...", of course.
Post Reply