Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
  • Programming in Processing (java)
    • Starting with Processing (en)
    • Variables (en)
    • Classes and Objects (en)
    • Events und Functions (en)
    • Writing our own Functions (en)
      • Functions (de)
    • Random Numbers (en)
    • Conditionals (en)
    • Loops (en)
    • Nested Loops (en)
    • Coordinates (en)
    • Arrays and Lists (en)
    • SVG + Images (en)
    • Motion and Temporality
    • Gesture Interactions
    • Using bitmaps as modifiers
    • Vectors
    • Animation
    • Animation 2
    • Simple Collision Detection
    • Sound
    • Typography

/
Writing our own Functions (en)

Writing our own Functions (en)

Oct 28, 2020

Analytics

Loading data...

deutsche Version

Syntax of functions

There are two parts of a function. First there is the the function declaration, where the name of the function along with its properties and behaviors are defined. Next there is the function call, where the function is then actually executed. Functions may be called multiple times.

Functions can also accept input values (called parameters) that are sent to the function when called. These input parameters can change the behaviour of the function.

Functions can also return a value. This means when we return to the line where the function was called, it has now carried a number with it. In processing, functions that have no return value are declared with “void” return type.

Function Declaration

This is how the function is named, its return type and parameters defined with sudo code.


datatype functionName (parameter,.....)

{

<function contents>

<possible return value>

}


Function call 

This is how we execute a function:

functionName(parameter,.....) ;


Function Examples

This example shows how to write functions with return values.

void setup()
{
  println(addition(1,2));
  println(addition(2,10));
}
 
int addition(int parameter1, int parameter2)
{
  return parameter1 + parameter2;
}


This example shows how to use functions for creating legible and simple blocks (modularization).

void setup()
{
  size(500,300);
  stroke(255);
  strokeWeight(3);
  noFill();
}
 
void draw()
{
  background(0);
  smiley(100,height/2);
  smiley(250,height/2);
  smiley(400,height/2);
}
 

void smiley(int x, int y)
{
  println("smiley");
  ellipse(x,y,100,100);  // head
  
  ellipse(x - 20,y - 10,10,15);  // left eye
  ellipse(x + 20,y - 10,10,15);  // right eye
   
  arc(x,y,60,60,radians(20),radians(180-20));  // mouth
}


And here is our first interactive program:

void setup()
{
  size(300,300);
  background(0);
}
 
void draw()
{}
 
void mousePressed()
{
  println("x:" + mouseX + ", y:" + mouseY);
}

Exercise 4

Write a sketch where a unique shape follows the mouse position. When you click the mouse, the shape should change in some way (form, colour, size).  


, multiple selections available,
{"serverDuration": 68, "requestCorrelationId": "2affd8d3b1c8426493eb4811750068bb"}