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.
Page Index Toggle Pages: 1
sub routines (Read 536 times)
sub routines
Apr 19th, 2010, 12:56pm
 
I am a novice to programming in general and would like to ask a basic question.  How do you create a sub routine?  Basically I am trying to create a program that will build a cube and then replicate it several times, so I wrote the code and would like to place it in a sub routine and then be able to call it and even pass arguments to it.

Any help is appreciated.
Re: sub routines
Reply #1 - Apr 19th, 2010, 3:05pm
 
See the link at the top of the page that says 'Learning'?

Use it Wink

functions are easy enough to create.  Outside of draw and setup you just need the following:

Code:
[return type or 'void'] functionName([argumentType] [argument reference]) {

 // function statements

// if return type is specified then include a return statement;

}


So for example here's a function that returns an int and takes two arguments:

Code:
void setup() {
 size(100,100);
 println(myFunction(3,4));
}

int myFunction(int firstNumber, int secondNumber) {
 return firstNumber + secondNumber;
}
Page Index Toggle Pages: 1