INTRODUCTION TO COMPUTATIONAL MEDIA
Modular Organization :
Writing your own functions .
Software can become so complicated that good organization is not just nice,
it is necessary. The software that you write may never become that complicated
but everyone will be much happier if you learn to write your code in modular
blocks. The simplest way to make your code more modular is to organize your code into "functions" (aka "handlers", "routines", "subroutines"
,"behaviors" and "methods" ).
In crafting these blocks, the holy grail is REUSE of your code and the criteria
is ENCAPSULATION OF COMPLEXITY.
- Practical Advantages of Modular
Code
- Creating Your Own Handler
- INPUTS
- OUTPUTS
- More Events Supplied by Processing
- Multidimensional Arrays
Practical Advantages of Modular Code:
- Easier to Read Your Code: It is is easier to see the overall flow of the program if you use single line functions calls hide the details of the flow in the functions themselves.
- Saves typing: If you are going to execute the
same set of commands (even with slight variations) you can write the lines
once and call them from many places. For instance you might want to draw a face on mousedown, within the main loop and at startup.
- Changes are easier: Change code in one place and
and it changes for all the places it is called.
- Do Hard Thinking Once: After you fight through some tough code, you
never have to look at it again. All you have to remember is the interface
(inputs and the outputs). This is the encapsulation of complexity.
- Easier to Copy and Paste: If your interface (inputs and output) are
complete enough you can copy and paste handlers between your processing projects.
If you have done a good job of making the handler, everything that might vary
is made into a parameter variable . Every variable
is then passed to the function as a parameter instead of instance variables
from the top of your program. If you use instance variables instead of parameter variables, the function will only work in your program or one with instance variables by the same name.
Creating Your Own Handler
- We have already dealt with handlers setup() and draw() These processing automatically calls these functions. You might look for more of these functions like mousePressed( ). It turns out that you can call out your own messages and make your own fucntions
to receive them.You can name the function anything you want and call it out anytime you want. As with naming variables, you will have much more readable code if the name
of the handler describes what happens in the code. Take some time to think
of a good name and change the name when the function changes. The usual format for a fucntion looks like this
- void myFuctionName() {
- //stuff that should happen when this function is called
- //stuff that should happen when this function is called
- }
Meanwhile these functions can be called from anywhere like this:
Below we will learn to use inputs and output with these functions
- int myAdder(int _a, int _b){
- int c = _a + _b;
- retrun c; //this is how you pass something back to the calling line of code.
- }
- Becuase this function will give us something back when it is called we first specify the type of the returned value with "int". This is in the place of "void" that we have been using for fuctions that don't return anything.
- We are providing variables in the form of parameters in the parenthesis after the function name. This allows you to fine tune the functions work and thus make it more generally useful and thus reusable.
and to call that
- int biggerNum = myAdder(firstNumber, 8); //feed the function a 7 and an 8 and put the result (15) is in var biggerNum
- On the calling side you need to provide the correct type and the correct number of parameters. On the calling side these can be hard numbers or variables. There is no need to specify the type on the calling side.
- The parameters you provide will be copied into new variables, usually of a different name as specified in the input parenthesis of the program.
INPUTS
- You seldom want to execute exactly the same code.
- Usually there are small variations. This sounds like a job for variables.
- When you call the handler you get to send a little additional information
using parameter variables. This allows you to customize the function.
- The extra information is added after the name of the function
- You are used to this with built-in functions that you have already been using in processing like ellipse(x,y,w,h);
- You have to specify the type of data you are expecting to come into the variable right before the variable name.
- If there is more than one parameter variable that you want to pass along,
you separate them by commas.
- The extra stuff on the calling side gets transferred into the parameters
on the receiving side according to the order they were sent.
- The variables names on the receiving side will probably not be the same name as
the ones on the sending side but they still get transferred in order.
- All parameter variable are local to the function. If you want to widen their scope, you have put them into instance variables.
- It is a stylistic convention to use a "p" or an underscore character
("_") at the beginning of the variable name to remind you that they
are parameters and short lived. For example "pWhichColor" or "_whichNum."
- In general you want to use all parameter variables and no global variables.
This makes your code very transferable.
- Don't turn this into a fetish where you can't get anything done until you
have made only perfectly clean and transferable code. You can't predict the
future uses so don't become so "reuse" crazy that you get distracted
trying. When you need very reusable code, the benefits will be obvious and
you will do it out of immediate self interest.
OUTPUTS
- It is often the case that you want to get something back from a handler.
- For instance you might have a handler that converts letters from lowercase
to uppercase. It needs to give you back the upper case when it is finished.
- You do this using "return." You put return followed
by what you want to return.
- You must specify the type of thing you will be returning before the name of the fuction.
- Finally the mysterious void will be explained. Void means that the function has no output.
- Return ends the handler. If you have more lines below the return, they will
never happen.
- Return only returns one thing. If you want to return multiple things, you
have to put them in an Array.
- Sometimes you will hear people call a handler that does not return anything
a command and something that does return something a function.
More Function Calls Supplied by Processing
- We have already made functions to handle function calls for "SetUp" and "Loop"
- You should look at the other function calls that Processing makes for things like mouse and keyboard events.