Page 1 of 1

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

Posted: Mon Jun 17, 2019 10:23 pm
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.

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

Posted: Mon Jun 17, 2019 11:52 pm
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)

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

Posted: Tue Jun 18, 2019 1:25 am
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)

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

Posted: Tue Jun 18, 2019 6:11 am
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

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

Posted: Tue Jun 18, 2019 7:59 pm
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'

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

Posted: Tue Jun 18, 2019 8:43 pm
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.

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

Posted: Tue Jun 18, 2019 8:44 pm
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.