An Introduction To Programming With Processing

The println() function

Function calls
println() is a special type of command known as a function. Sometimes a function is also referred to as a subroutine so how the term is used from one language to another may vary but regardless of what you call it the purpose of a function is generally the same in any programming language, a portion of code (usually a single word followed immediately with parenthesis) that exists within a larger body of code (in our case this could be the sketch we are creating) and performs a specific task within the context of the program but is also independent of the program. What this means is that we have not defined the function println() we are simply using it as it has been defined by the people that developed Processing. In other words we have not told the computer what to do when it comes across println() in our Hello World 1.0 program. This means that the main body defining how println() acts in our program is not defined within our program but exists independently of our program, somewhere else on your computer's hard disk drive which would typically be where you installed Processing. If we were to take another look at the Hello World 1.0 program, we can see the function println() is telling the computer, that the sketch is running on, to print whatever is inside it's parenthesis. We refer to data we input into a function through it's parenthesis as a parameter. Some other higher level languages might refer to parameters as arguments, but Processing adheres closer to C style code by using the term parameter as an input for functions and the term argument is used to describe what happens to a parameters through evaluation (which is something we discuss in more detail later). The parameters our println() function accepts in this case is the string of characters that make up the words “Hello World”. There are two points worth noting here, firstly we have input data into the println() function by using it's parameter options and secondly it has returned data back to us, that being what it has printed to the Text Area. When a function is used in this way we are said to be calling a function. Functions are the building blocks of Processing and we will be using them regularly and even creating our own.

Syntax and Syntax Errors

The arrangement of components within a statement is exceptionally important in programming. Unlike in natural languages, where sentences can be structured in several different configurations and still have the same meaning. In programming a specific syntax exists for all languages and must be adhered to or your compiler/interpreter might throw an exception or a syntax error. Syntax errors are generally quite common when you first start programming, it is easy to forget to place a semi-colon at the end of a statement or close parenthesis that have been left hanging open. Errors like these are often easy to debug, particularly when using the PDE. For example, if we were to make a mistake in our Hello World 1.0 program and forget to close the open parenthesis, when trying to run the sketch we could get an error looking something like this...

Syntax Errors
Syntax errors are common when one first starts programming, but with the help of the debugging console they can be relatively easy to identify.
As you can see debugging the program in this case is really quite simple, in fact the PDE tells us exactly where the error is. In larger programs however it might not be so obvious where the problem lies, although the PDE will try to help you in tracking down a bug where ever it can. Some useful pointers to remember when constructing statements in Processing follow, these pointers could help in avoiding syntax errors. Always end statements with a semi-colon. All parenthesis (), brackets [] and braces {} that are opened with their corresponding left characters must be closed with the right version of the same character. None of these sets are interchangeable, but some of these sets are nestable eg (code(code)code), {code(code)code} but (code] is not allowed. A String of literal characters (such as “hello world”) must exist between double quotes. Single quotes are reserved for the char data type (discussed later). Look it up in the Processing reference when you are uncertain, which can be accessed online at http://processing.org/reference/ or from the PDE, for offline viewing, click :
Help → Reference

Of course there are many other points worth noting on syntax and program structure but we will get to these in due time, for now there's no need to get ahead of ourselves.

Logical Errors

Logical errors are errors that do not cause the program to halt, crash or throw an error but will cause the program to act in an unexpected manner or produce unintended results. Logical errors can therefore be difficult to track down and rectify because the PDE does not indicate the specific location of an error. Keeping track of your data through documentation and organizing it into small manageable chunks can be one method of avoiding logical errors. If your program seems to have a logical error you might have to use the println() function to track the values you were hoping to have your program return to you, we'll take a closer look at this technique when we start creating our own sketches.