1 of 24

Class 4

2 of 24

Functions

  • Functions are a block of code {…} that we name.�
  • They are useful because they allow us to use them in multiple places in our code without retyping them.

  • They are useful because they can be modular and allow for flexibility.

  • They increase the readability of our code.

3 of 24

Functions - we know them

  • We have been using functions from week 1 line(), size() �
  • We have been declaring functions from week 2 void draw() {…}�
  • Now it’s time to thoroughly understand it.

4 of 24

Functions - using

  • Some functions take no arguments and return no value noStroke();�
  • Some functions take arguments but return no value line(x,y);�
  • Some functions take no arguments and return a value x = hour();�
  • Some functions take arguments and return a value x = random(0,5);

5 of 24

Functions - declaring

  • Function declaration has the following structure:

return type name (arguments) {our code}��• return type - The result of our function void, int, float, char (void means nothing returned)��• name - same as variables , no funny stuff.��• arguments - parameters for our function��• {our code} what we want to happen when it is called

6 of 24

Functions - declaring

  • Declaring a function with no return value and no arguments:��void drawFace(){� ellipse(50,50,100,100); � line(30,40,50,70); // nose� ellipse(70,70,10,10); //eye� ellipse(80,70,10,10); //eye�}

  • To call this function - drawFace();

7 of 24

Functions - declaring

  • Declaring a function with no return value and arguments:�
  • place the data type and names of the argument in the parentheses :��void drawFace(int centerX, int centerY){� ellipse(centerX, centerY,100,100); � line(centerX, centerY-5, centerX-5, centerY); � ellipse(centerX, centerY-5, centerX-5, centerY); ellipse(centerX-5, centerY, centerX, centerY-5); �}

  • To call this function - drawFace(50,50);

8 of 24

Functions - declaring

  • Declaring a function with a return value and arguments:�
  • place the data type of the returned value before the name ��int calcArea(int rectWidth,int rectHeight){� return rectWidth *rectHeight;�}

  • To call this function - x= calcArea(50,60);

9 of 24

Functions - scope of variables

  • variables that are declared on top are available everywhere in the program, they are “Global”

�int i=5;

void setup(){� println(i);

}��result - prints 5

10 of 24

Functions - scope of variables

  • variables that are declared inside one function (or any block of code) are only available within that function, they are “local”

void setup(){

int i=5;

}�� void draw(){

println(i);

} �

result -Processing will not compile and say i is unknown

11 of 24

Functions - scope of variables

  • variables that are declared inside a function (or any block of code) do not retain their value between calls:

void draw(){

int i=0;

i=i+1;

println(i);

} �

result - repeatedly printing “1”

12 of 24

Objects

  • Functions are cool, they let us organize our code into smaller units.�
  • But we still need to handle all the data in a central location (global variables).�
  • When you do stuff that has multiples, it becomes cumbersome to manage all the variables.��

13 of 24

objects

  • For example if we had a program with 3 bouncing balls we would need variables to remember position X and Y, color R, G and B and speed all times 3 = 21 variables.

  • Wouldn't it be nice to have an entity that consolidates all the data we need for each ball? then we would only need 3 of these “meta” variables (one for each ball).

  • Wouldn't it be nice if that entity could also hold actions (functions) so we could say something like “ball one, draw yourself”.

14 of 24

Objects

  • Just like function allow us to consolidate multiple statements and name them, the object allows us to consolidate functions and data and encapsulate them.

  • Using objects is very easy and intuitive, defining your own objects can sometimes require a fresh look at the structure of your code.

15 of 24

Objects

  • There are 3 steps in the life cycle of an object:
      • Declaring
      • Initializing
      • Using

�Let’s assume someone has created an object called “ball” that knows how to move and bounce:

16 of 24

Objects - declaring

  • Declaring . This means creating a variable to hold the object . It is global and goes on the top.��Remember variable declaration ?� float myColor;

�Objects are declared the same, but we put the object’s (class) name as the type:�� Ball myBall;

17 of 24

Objects - initializing

  • Initializing - this means giving birth to the object and also setting up any initial settings by means of parameters.��This takes the form of:

variable name = new class name(parameters)

So, for our ball example, we probably have to set the initial location of the ball:

myBall = new Ball(100,100);

18 of 24

Objects - using

  • Using - this means asking the object to perform the functions that it was designed to do.��This takes the form of :

variable name . function name ( parameters);

  • So, for our ball example, maybe it has a function to change the color of the ball :

myBall.changeColor(255,0,0);

19 of 24

Objects creating/defining classes

  • In the case of our ball example, now that we see how we would use it, let’s see how we would create this object. This definition, or set of functions and data that will make up the object is called “class”

  • Some people find the cookie cutter analogy helpful, you make one cookie cutter (class) and it can create multiple cookies (objects)

20 of 24

Objects - defining classes

  • The main challenge in objects is to define the class. This has 4 parts:

      • The class name
      • The data (variables)
      • A constructor
      • Functions

21 of 24

Objects - class name

  • Every class needs a name, that will be used as the data type for variables that store it.

Remember? Ball myBall;�

• The syntax for naming your class is :

class name { class definition statements }

( No parentheses and no “void” )

22 of 24

Objects - data

  • Most classes have variables declared in them to store information about the object, these variables are local to the class and are sometimes called instance variables or class variables.

class Ball {

int xPosition;� int yPosition:

…� }�

If you want to access these variables from another part of your code you can use the dot “.” notation i.e. myBall.xPosition

23 of 24

Objects - constructor

  • All classes have one special function that is automatically called when the object is initialized with “new”. It always has the same name as the class. �
  • It is declared like any other function but without the void, because it cannot return anything.

class Ball {

int xPosition;� int yPosition:

Ball(int _xPosition, int _yPosition ){� xPosition = _xPosition;

yPosition = _yPosition;� }

}

You can pass arguments like any function and they will be passed by “new”

24 of 24

Objects - functions

  • Classes can have many functions with many names, they are similar to regular functions.

class Ball {

int xPosition;� int yPosition:

Ball(int _xPosition, int _yPosition ){� xPosition = _xPosition;

yPosition = _yPosition;� }

void changeColor(int newColor){

fill(newColor);

}

}

You can pass parameters like a regular function and the syntax to call this function would be :

ourBall.changeColor(255);