It is currently Sat Aug 24, 2019 4:36 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: [PB5.70] Possible bug calling procedures and variables?
PostPosted: Mon Jun 17, 2019 10:23 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Mar 10, 2013 3:01 pm
Posts: 631
Location: Portugal
Hello!

Is this a bug?
Code:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.70] Possible bug calling procedures and variables?
PostPosted: Mon Jun 17, 2019 11:52 pm 
Online
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3022
Location: Boston, MA
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.70] Possible bug calling procedures and variables?
PostPosted: Tue Jun 18, 2019 1:25 am 
Online
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3568
Location: Utah, USA
Code:
;// 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)

_________________
Image


Last edited by Demivec on Tue Jun 18, 2019 7:06 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.70] Possible bug calling procedures and variables?
PostPosted: Tue Jun 18, 2019 6:11 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3640
Location: Berlin, Germany
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

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


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.70] Possible bug calling procedures and variables?
PostPosted: Tue Jun 18, 2019 7:59 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 882
@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


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.70] Possible bug calling procedures and variables?
PostPosted: Tue Jun 18, 2019 8:43 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jun 01, 2012 12:20 am
Posts: 140
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.70] Possible bug calling procedures and variables?
PostPosted: Tue Jun 18, 2019 8:44 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3640
Location: Berlin, Germany
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.

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


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 19 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