FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   global variables vs function calls
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: global variables vs function calls  (Read 352 times)
Jerronimo

WWW
global variables vs function calls
« on: Sep 9th, 2003, 5:41pm »

how come mouse state is retrieved from a variable  
  mouseX, buttonPressed, etc
while time is retrieved from a function?
  second(), millis(), etc?
 
however buttonPressed() is an overridable function, and yet second() isn't.  I would think that to make it consistant, the time should just be stored in global variables, much like the mouse functions.
 
Is there a reason for this inconsistancy, or is it just an artifact of a previous idea?
 
 
fry


WWW
Re: global variables vs function calls
« Reply #1 on: Sep 9th, 2003, 11:46pm »

second() is a function because there's a good bit of overhead to calling it. so we can't just run it by default.  
 
mouseX/mouseY are variables because they're used incessantly, and there's (virtually) no overhead for storing their values.
 
buttonPressed() is overridable so that you can change what happens right when a button is pressed.
 
second() is not overridable because functions can be made faster if they're set to be functions that are never overridden (declared 'final') and i couldn't conceive of a reason why someone would override the behavior of getting the seconds. and for such a strange situation, they can prolly deal with calling it something else, rather than slowing it down for everyone else.
 
millis() is a function because the variable might be updated more than once while inside of loop() (see postings about this elsewhere on the board). and it can't be magically updated mid-loop. the other time stuff also falls prey to this fact, though less so.
 
it's an artifact of how programming languages work, if anything. i'd like to have time as a variable, for instance, but java's time implementation is a little goofy and overhead-bound.
 
Jerronimo

WWW
Re: global variables vs function calls
« Reply #2 on: Sep 10th, 2003, 12:48am »

I had a feeling that was the reason (the overhead) but wanted to be sure...
 
Pages: 1 

« Previous topic | Next topic »