We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › noob: Why this variable in setup()
Page Index Toggle Pages: 1
noob: Why this variable in setup() (Read 1224 times)
noob: Why this variable in setup()
Aug 20th, 2009, 5:22pm
 
Can someone tell me what purpose endY = 0 in setup() serves? Removing it seems to have no effect on the program. If it is there to initialize endY to 0 for later use in draw(), why can't that be taken care of in the declaration at the top.
Thanks.


int endY;

void setup() {
 size(200,200);  
 frameRate(5);
 endY = 0;
}

void draw() {
 background(255);
 for (int y = 0; y < endY; y+=10) {
   stroke(0);
   line(0,y,width,y);
 }
 endY += 10;

 if (endY > height) {
   endY = 0;
 }
}
Re: noob: Why this variable in setup()
Reply #1 - Aug 20th, 2009, 10:39pm
 
I would be cynical, I would say it is useless, just eating CPU cycles.
I am not, so I will say the performance hit is not visible, and somehow it has a documentation purpose (indicating the initial value). Could also have set the value at declaration time. But in both cases, it is not necessary because class fields (and thus Processing "global variables") are set to a default value: null for objects (including strings and arrays), 0 or 0.0 for numbers, false for booleans, etc.
Note that's not the case for local variables (declared in a block, including function definitions): the compiler will warn you against use of uninitialized variable usage.
Re: noob: Why this variable in setup()
Reply #2 - Aug 20th, 2009, 11:09pm
 
Thanks for the response. That seems to make since.
Re: noob: Why this variable in setup()
Reply #3 - Aug 21st, 2009, 5:53am
 
If you're learning out of a book, a lot of times an author will use the same sketch over and over and add things to it along the way. The variable endY may have a different purpose later on, where you'll be initializing it to something else in the setup(), and so the author wants to keep things organized properly for the earlier stages. This may or may not be true, so take this comment in addition to what PhiLho said.

Let me also say this. Don't be too overly concerned with knowing the exact purpose of every little line of code when you're learning. You've obviously noticed that taking out that initialization in setup() doesn't affect your sketch. So that's part of learning right there, being observant on how things affect your sketch and not necessarily why things affect your sketch.

I used to try and learn programming languages by sitting down with a book and reading from cover to cover before I did any coding at all. I wanted to know everything there was about the language before I started coding. What a fool I was! This is the equivalent of trying to learn a foreign language by sitting down with a dictionary and just learning all the words. It's better to learn in context, kind of like learning a foreign language through immersion. You just get your feet wet, and figure out the details later. I'm experiencing this big time with the iPhone SDK right now. Objective-C is bizarre when you're coming from Java and C++, and it's a vast language, and it's really useless to try and learn all the details before you just start trying to make iPhone apps. Then as your needs change, you dig deeper to find the answer. It'll be the same with Processing. Sorry this is long-winded, but hope this helped in some way!

Smiley
Re: noob: Why this variable in setup()
Reply #4 - Aug 21st, 2009, 1:28pm
 
Thanks for your input. It helps. I will keep that in mind.

I can relate to what you are saying. All the other books I have read in the past couple of years while trying to learn C++ and JavaScript did not help me much. I read at least 15 thick books. They seem to teach the subject backwards by focusing too much on learning all the language details instead of creating a piece of software. Only one book, Ivor Horton’s Beginning Visual C++ 2005 walked me through using Visual Studio and C++ to create an actual piece of software but it was still a little too hard for me at the time so I gave up.

Now I am reading Learning Processing by Daniel Shiffman. I get quick visual feed back from short programs. Processing and this book might be the stairs I have been looking for to climb this mountain. I figure if I have a thorough enough understanding of the principles I can return to JavaScript and later C++.
Re: noob: Why this variable in setup()
Reply #5 - Aug 21st, 2009, 5:42pm
 
If you're looking for a good, practical C++ book, I highly recommend Beginning C++ Through Game Programming by Michael Dawson. He has you creating working games right from the beginning, so you're always programming for a reason, for some sort of interactivity, rather than just printing things out all the time about variable statuses.

Oh and I highly, highly, HIGHLY recommend OpenFrameworks for programming in C++. The developers really tried to make it just like Processing, and named a lot of the drawing and other methods the exact same as Processing, so it's super easy to go from one to the other. It's set up just like a Processing sketch, with setup() and draw() methods and all that. You should definitely check that out if you are into C++ using OpenGL.
Re: noob: Why this variable in setup()
Reply #6 - Aug 22nd, 2009, 11:27am
 
I have a 3d animation background and someday I want to design virtual experiences. Thanks for the suggestion of Beginning C++ Through Game Programming by Michael Dawson. I will put it on my future investigation list. It sounds really interesting. A couple of years ago I bought this book Introduction to 3D game programming with directX 9.0c Frank D Luna. Since I hit a learning wall with C++ I could not really get started with this book, so it sits on my shelf.

I discovered processing just a few weeks ago and shortly after that stumbled on OpenFrameworks. Apparently it's really new. I could not find any books about it. Thanks for mentioning it, it's good to know someone recommends it.
Re: noob: Why this variable in setup()
Reply #7 - Aug 23rd, 2009, 12:44am
 
There is something puzzling me about openFrameworks. The Web page hasn't much changed in ages, still mentioned "in pre-release, and heading towards a public release" and beside a bunch of videos, I see no real information.
So I wonder how people can even use it Is joining the mailing list the mandatory step to be in the secret
Re: noob: Why this variable in setup()
Reply #8 - Aug 23rd, 2009, 1:57am
 
nope it is not mandatory and no secrets at all.
development and the community is very much alive.
you can find more info from here:
http://www.openframeworks.cc/about
Re: noob: Why this variable in setup()
Reply #9 - Aug 23rd, 2009, 3:16am
 
Lenticular wrote on Aug 21st, 2009, 1:28pm:
Now I am reading Learning Processing by Daniel Shiffman. I get quick visual feed back from short programs. Processing and this book might be the stairs I have been looking for to climb this mountain. I figure if I have a thorough enough understanding of the principles I can return to JavaScript and later C++.


From my experience Processing is a really good place to learn the ropes.  I'd done a lot of procedural coding in ActionScript, and before that web stuff using PHP and a little Python, but had never considered writing classes until I started working with Processing...  Now I've got my head round the concepts involved I'm comfortable writing classes in ActionScript and feel ready to apply the same principles in other languages... though TBH it will be hard to drag myself away from Processing given its versatility...
Re: noob: Why this variable in setup()
Reply #10 - Aug 23rd, 2009, 3:39am
 
frankBerg wrote on Aug 23rd, 2009, 1:57am:
you can find more info from here:
http://www.openframeworks.cc/about

How come the top navigation list of this page isn't in the home page The latter makes the site look like it is empty...
Thanks for the link!
Page Index Toggle Pages: 1