home/ atoms/ processing-custom-functions

Custom functions in Processing encapsulate reusable code blocks with parameters and return values

Functions group code into named, reusable units. A void function returns nothing; other functions declare a return type (int, float, etc.) and use return to send back a value. Parameters are typed local variables that receive values when the function is called. Multiple parameters are separated by commas. Function overloading allows the same name with different parameter signatures. Abstraction through functions means callers need not know the implementation — just the inputs and outputs. This principle parallels how Processing’s own built-in functions (line(), ellipse()) hide their drawing details.

Examples

void drawX(int gray, int weight) { stroke(gray); strokeWeight(weight); line(0,5,60,65); line(60,5,0,65); }

Assessment

Write a function drawGrid(int cols, int rows, int cellSize) that draws a grid of rectangles at the correct positions; call it three times with different parameters.

“Each function should be a unit of code that clearly expresses a single idea, calculation, or unit of form.”
corpus · processing-handbook-no-login-mirror-pdf-reas-and-fry · chunk 45