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 › naming functions
Page Index Toggle Pages: 1
naming functions (Read 728 times)
naming functions
Dec 14th, 2009, 8:39am
 
this is probably an exceptionally easy question but I am stumped  Undecided...

i need to create a function (my_function) which contains 2 arguments, these 2 arguments must return integers (based on them)

how would i go about doing this

thanks
Re: naming functions
Reply #1 - Dec 14th, 2009, 9:21am
 
When you say 'return' you presumably mean the two arguments must be integers; not that the function returns integers.  So:

Code:
void setup() {
 size(10,10);
 // call the function:
 myFunction(4,2);
}


// function definition:
void myFunction(int firstArg, int secondArg) {
 println(firstArg);
 println(secondArg);
}


The keyword 'void' just means the function doesn't return a value.  You follow that by the function name and then open a bracket.  Then you add your arguments.  Each argument should be preceded by the type that is expected (e.g. int, float, String, boolean, Object etc) and arguments are separated with commas.  You then close the bracket and open a curly brace and finally close the function with a closing brace.  You can then reference the value of the arguments between the function's braces (and do stuff with them) using the argument names.

Now if you did want your function to return a value you replace 'void' with the type that the function returns and then must include a return statement that uses the appropriate type.  For example:

Code:
void setup() {
 size(10,10);
 // call the function:
 println(myFunction(4,2));
}


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