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 › functions question
Page Index Toggle Pages: 1
functions question (Read 609 times)
functions question
Mar 27th, 2008, 12:42pm
 
hello. I new to code... something simple I do not quite understand.

How come you call a function before it has been declared. For example :

-----------

void setup()
{
 size(200, 200);
 background(51);
 noStroke();
 smooth();
 noLoop();
}

void draw()
{
 draw_target(68, 34, 200, 10);
 draw_target(152, 16, 100, 3);
 draw_target(100, 144, 80, 5);
}

void draw_target(int xloc, int yloc, int size, int num)
{
 float grayvalues = 255/num;
 float steps = size/num;
 for(int i=0; i<num; i++) {
   fill(i*grayvalues);
   ellipse(xloc, yloc, size-i*steps, size-i*steps);
 }
}


-----


When the draw function is called it in turn calls the draw_target function which is parsed the arguments 68, 34, 200, 10 etc. The function is then declared below. But how can we call a function before it has been declared. I don't understand? Sorry. Thank you
Re: functions question
Reply #1 - Mar 27th, 2008, 12:59pm
 
When you're programming in C then you of course write the stuff you're calling before you use it.

But in Java, the computer reads the whole program before running it. This allows us to put the more important bits of code, like the main loop of the program at the top, making it easier to read - rather than having to scroll to the bottom of the code.
Re: functions question
Reply #2 - Mar 27th, 2008, 1:02pm
 
aha!

Simple.

Thank you so much.
Re: functions question
Reply #3 - Mar 27th, 2008, 1:05pm
 
and of course I could put all my functions at the top of my code for a good house keeping no?
Re: functions question
Reply #4 - Mar 27th, 2008, 1:47pm
 
learncoder wrote on Mar 27th, 2008, 1:05pm:
and of course I could put all my functions at the top of my code for a good house keeping no


Yes, that is possible. Although I'm not sure, if it makes a lot of sense, since you'll have to scroll past the functions every time you want to get to the main part of the code.

What I often do, is to put all the supporting functions into an entirely new tab. This way you can keep the main part of the code you're constantly working on separate from the parts which you write once and don't need to edit any more.
Re: functions question
Reply #5 - Mar 27th, 2008, 1:55pm
 
a good idea! I will try this. I was unaware you could have multiple tabs for each sketch. Thanks you for your advice.
Page Index Toggle Pages: 1