WEEK: 12

Types of functions

Besides having built-in functions and user-defined functions, there are functions with and without parameters. They look differently when they are defined and called. What do they look like?

Functions without Parameters

Recall that functions are created to combine multiple lines of code into one. The function below creates both a circle and a square. When the function CircleSquare is called, a single circle and square are drawn to the screen in a specific location.

    function CircleSquare()
    {
        circle(100,200,25);
        square(250,350,50);
    }

Functions with Parameters

In the previous example, the circle and square are drawn in only one location. However, we can send in parameters to change the location of the circle and the square.

Parameters are variables that are passed into the function.

    function CircleSquare(circleX, circleY, diameter, squareX, squareY, sideLength)
    {
        circle(circleX, circleY, diameter);
        square(squareX, squareY, sideLength);
    }

In the next section, let’s look closer at why programmers create functions.

See the Pen p5.js Functions with/without parameters by Michael Cassens (@retrog4m3r) on CodePen.


Previous section:
Next section: