Home CPSC 220

Programming with Functions


Splitting a Program Into Functions

When writing a program, it's good to break it down into multiple functions. This makes your code easier to write, read, test and debug.

A function should have a specific job, and generally shouldn't be too large.

When planning a program, deciding how to break it into functions is an important task.


Example: Rock, Paper Scissors

To practice designing a program with functions, how could we write a player vs. computer Rock, Paper Scissors game? What tasks can be split into separate functions?


Random Numbers

To implement this game, we will need some element of randomness. To get random numbers in Java, we can use the Random class as in this example:

import java.util.Random;

public class RandomNumbers {

    public static void main(String args[]) { 
        Random generator = new Random(); 
        for (int i = 0; i < 10; i++) {

After importing the Random class, we can create a variable of type Random. The "nextInt" function on this variable returns an integer from 0 up to one less than the parameter. So the program above will print 10 random numbers from 0-9.


Implementing Functions

Once we have split our task into functions, we can start to implement them, filling in the bodies with code.

How could we implement each of the Rock, Paper, Scissor functions?

import java.util.Scanner;
import java.util.Random;

public class Example {
    /* readInput takes a prompt which is printed out,
       and also a max, it returns to you a number from
       1 up to max 
    public static int readInput(String prompt, int max) {
        int choice = 0;
        while (choice <= 0 || choice > max) {
            Scanner in = new Scanner(System.in);
            choice = in.nextInt();
        return choice;
    public static int getComputerThrow() { 
        Random generator = new Random();
        int value = generator.nextInt(3) + 1;
        System.out.print("Computer threw");
        switch (value) {
            case 1: System.out.println(" a rock."); break;
            case 2: System.out.println(" paper."); break;
            case 3: System.out.println(" scissors."); break;
        return value;
    public static String compareThrows(int player, int computer) {
        int difference = player - computer;

        if (difference == 0) {
            return "Tie!";
        } else if (difference == -1 || difference == 2) {
            return "Computer Wins!";
        } else if (difference == -2 || difference == 1) {
            return "User Wins!";
        } else {
            return "Impossible!";
    public static void outputResult(String result) {
    public static void main(String args[]) {
        boolean playing = true;
        do {
            int user = readInput("Enter your throw (1) rock, (2) paper, (3) scissors", 3); 
            int computer = getComputerThrow();
            String result = compareThrows(user, computer);
            System.out.println("Play again? "); 
            int answer = readInput("Enter (1) play again, (2) quit", 2); 
            if (answer == 2) {
                playing = false;
        } while (playing);


Testing Functions

After writing each function, it's a good idea to test it out. It's much easier to test our program one function at a time, then test the entire program at once.

One way to do this is to use the main function for testing. After each function we write, we can add code in main to test it out.

Then we can fill in the code for main last and test the entire program.

Generally, you should work on the code a little at a time, then compile and test your program. This is called stepwise refinement.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.