Functions are Awesome
Functions are your best friend, they are also known as methods They basically are a set of instructions that you encapsulate for later use. Once you define a function, you can call upon it to execute a set of instructions.
Besides sides executing instructions, functions can be made to return a result in a variable/datatype of your liking. Sort of like a calculator.
In processing when you don’t need to return a variable or datatype, you simply insert the term ‘void’ before your choosen function name. When you need to return a certain datatype, specity it. Say you needed an int value return, so instead using ‘void’, you would use ‘int’ before you define the function’s name.
The Nuances Of Declaring Functions
// This function does not do anything
// Notice the void and buildArchitecture()
void buildArchitecture() {
// 'void'
/*
When you choose to 'void' a function, it just
means that you don't return anything, rather
only instructions are executed
*/
// buildArchitecture()
/*
Wouldn't it be cool if there was function that
could buildArchitecture for you? Well,
if it doesn't exist it probably would consist of
thousands of other functions.
*/
}
// Functions can have parameters.
// If you need information before you
// excuting a set of instructions, then use parameters.
// --- Make sure you specify the datatype
// --- Before you provide a parameter name
void buildArchitecture( int investmentmoney ) {
/*
Since investmentmoney is the paramter
you can use it any way you want.
*/
if(investmentmoney > 100) {
// Stuff to do when we have enough
// investment money
}
}
// Functions can return anything you want it to
// Make sure you return what is expected.
// In this case, it is the 'int' datatype
int daysUntilCompletion( int numberOfWorkers ) {
/*
Create you algorithms to calculate what
you need to return
*/
int daysCalculated = numberOfWorkers * 100;
/*
You can return any variable so long
as it is an 'int' in this particular case.
*/
return daysCalculated;
}