You are on page 1of 5

Introduction 3

Morphogenetic Interferences, Introduction to programming and Processing

Loops
Initialize
Executes once when loop begins

Evaluation
loop will be executed if test evaluates to true

Update
is executed at the end of each iteration

false
for (int i = 0; i < 100; i++) {

Iterations

true

i++

// these commands will be


executed until condition is
false

Console output
iteration: 0
iteration: 1

println(iteration: + i);

iteration: 2
[...]
iteration: 99

continue code

Introduction 3

Morphogenetic Interferences, Introduction to programming and Processing

Functions
Name
function Name, lowercase

void drawCircle() {

// these commands will be executed


when function drawCircle is
called.
ellipse (width/2, height/2, 100, 100);
}

// anywhere in the code, call function


drawCircle();

continue code

Introduction 3

Morphogenetic Interferences, Introduction to programming and Processing

Functions with arguments / parameters


Name
function Name, lowercase

Parameter list
declaration of variables separated by comma

position parameters are passed to function:


iteration 1: (100,100), iteration 2: (200,200), etc.

void drawCircle(int x, int y) {

// Parameters are only accessible


within the function itself!
ellipse (x, y, 100, 100);
}

// draw circles
for (int i = 100; i < 800; i+=100) {
drawCircle (i, i);
}
continue code

Introduction 3

Morphogenetic Interferences, Introduction to programming and Processing

Functions with a return value


return Type
a variable type

Parameter list
declaration of variables separated by comma

position parameters are passed to function:


iteration 1: (100,100), iteration 2: (200,200), etc.

int drawCircle(int x, int y) {

// Parameters are only accessible


within the function itself!
int rad = int(random(200));
ellipse (x, y, 2 * rad, 2 * rad);
return rad;
}
Console output
Circle 0 - radius 185

// draw circles
int radius;
for (int i = 100; i < 800; i+=100) {
radius = drawCircle (i, i);
println (Circle + i + - radius + radius);
}

Circle 1 - radius 15
Circle 2 - radius 23
Circle 3 - radius 76
[...]

Introduction 3

Morphogenetic Interferences, Introduction to programming and Processing

Variable
Scope

global variabels
are defined at the beginning
of the code, outside any curly
brackets
are accessible anywhere

// global variable declaration


int number = 40;
int maxRadius;
void drawCircle (int x, int y) {

local variabels

//local variabel declaration


int rad = int(random(maxRadius));

are defined within a code

ellipse(x, y, rad*2, rad*2);


println (number);
println (posY);
println (i);

block (within curly brackets)


are only accessible within
their own code block

void setup () {



size (800,600);
smooth();
noFill();
maxRadius = 300;


for (int i = 0; i < number; i++) {

int posX = int(random(width));

int posY = int(random(height));

drawCircle(posX, posY);

println (iteration + i);

}




}

println (i);
println (rad);
println (x);
println (maxRadius);

code blocks can be


functions
loops
conditions
[...]

You might also like