Skip to content

Functions and their Return Values#

So now that we know what functions are, we can also explain one thing we skipped over earlier: How did random() do its Job? Somehow we were able to write code like this:

float someRandomNumber = random(1, 20);
println(someRandomNumber);

Through some kind of magic, we'd have a number assigned to someRandomNumber. This is, what return values are for. Functions can return something back to the caller.

Example: Conversion#

Maybe I have a thermometer somewhere, and I need to convert from Celsius to Fahrenheit. That's a pretty simple conversion:

void setup() {
    printAsFahrenheit(20.0); // will print 68.0
}

void printAsFahrenheit(float inputCelsius) {
    float fahrenheit = inputCelsius * 9 / 5 + 32;
    println(fahrenheit);
}

That's not very versatile, though, because I can only ever print that result to the Processing Output Window. I can't do anything else with it. But If I gave my function the ability to return something, then I could use it in different places as well:

void setup() {
    float currentFahrenheit = celsiusToFahrenheit(20.0);
    println(currentFahrenheit);
    text(currentFahrenheit, 10, 30);
}

float celsiusToFahrenheit(float inputCelsius) {
    float fahrenheit = inputCelsius * 9 / 5 + 32;
    return fahrenheit;
}

Here we do the conversion in a function and return its result. The result goes back to the place where the function was called, and is assigned to a variable. With that variable, we can print or draw that value, we could also use it in further calculations or store it somewhere for long-term archival reasons.

The key difference here is, that celsiusToFahrenheit() has a return type of float - you may have noticed that we didn't write void in front of it. This, in front of the function name, is our return type, which are the same types you already know from variables and parameters. What you didn't know, was, that void is also a return type - just a return type of this function returns nothing.

Example: Dice Roll#

int diceA = 0;
int diceB = 0;

void setup() {
    size(300, 200);
    background(0);
    text("click anywhere and watch the text output", 5, 50);
}
void draw() { 
    // empty, only needed so the `mousePressed()` is run
    // see https://processing.org/reference/mousePressed_.html
}

int randomDiceRoll() {
    return floor(random(1,7));
}

String compareDice(int first, int second) {
    if (first < second) {
        return "Second Dice Wins";
    } else if (first > second) {
        return "First Dice Wins";
    } else {
        return "It's a tie!";
    }
}

void mousePressed() {
    if (diceA == 0) {
        println("\nIt's a new round!");
        diceA = randomDiceRoll();
        println("Dice A is now", diceA);
    } else if (diceB == 0) {
        diceB = randomDiceRoll();
        println("Dice B is now", diceB);

        // directly print the result
        println(compareDice(diceA, diceB));

        // reset for next round
        diceA = 0;
        diceB = 0;
    }
}

Terminal output of our dice roll program

In here, we have a randomDiceRoll() function! The name is great, because we don't really need to worry about what's in there. We're just rolling a dice. It returns an integer number to us. In line 18, we have a specialized function that takes two ints, it returns a String, containing which side won. We are free to do whatever we want with that result. We're choosing to just println() it in line 38.

We can now use these functions in the mousePressed() function, where we set our diceA and diceB variables. Clicking for the first time will set diceA, clicking for the second time will set diceB compare both, print the result and then reset both variables so we can then play another round.

compareDice() is a great example of a function that does not take pixel coordinates as parameters. We've had plenty of examples with that already.

Relevant excerpt from Learning Processing#

(the section starts at 3:57:00 and runs through 4:02:30, the video should start and stop on these automatically.)