# Practice: The need for functions

Print the scores of five football teams by copying and pasting the same code, and see why repeated code is a problem.

Novice · Java · Updated November 16, 2026

By Gregor Dzierzon

To follow along, create a new Java project in IntelliJ, add a class named `Functions01` with a `main` method, and paste the code below into `main`.

This program currently displays statistics for the top 3 college football teams in the NCAA for the past week's games (scores are not current, but you can change them if you would like). The statistics include the number of points scored in each quarter and each team's final score.

| Rank | Team | Q1 | Q2 | Q3 | Q4 |
|---|---|---|---|---|---|
| 1 | Georgia | 12 | 14 | 6 | 7 |
| 2 | Alabama | 14 | 17 | 10 | 14 |
| 3 | Ohio State | 21 | 10 | 14 | 7 |
| 4 | Michigan | 10 | 7 | 0 | 17 |
| 5 | Clemson | 14 | 6 | 8 | 10 |

### Instructions

The code to display the top three teams has already been written. Your task is to add the code to display teams 4 and 5.

Run `Functions01`.

Notice that the lines between `/* start repeatable code */` and `/* end repeatable code */` are repeated once for each team. Copy and paste those lines to display the team statistics for **Michigan** and **Clemson**. (Don't create a function yet.)

```java
String teamName;
int rank, q1, q2, q3, q4, finalScore;

/* start repeatable code */
// Georgia
teamName = "Georgia";
rank = 1;
q1 = 12;
q2 = 14;
q3 = 6;
q4 = 7;
finalScore = q1 + q2 + q3 + q4;

System.out.printf("(%d) %s \n", rank, teamName);
System.out.println("-------------------------------");
System.out.println(" Q1  Q2  Q3  Q4  Final");
System.out.println(" --- --- --- --- -----");
System.out.printf(" %-2d  %-2d  %-2d  %-2d  %d \n",q1,q2,q3,q4
                                                  ,finalScore);
System.out.println();
/* end repeatable code */
```

```java
/* start repeatable code */
// Alabama
teamName = "Alabama";
rank = 2;
q1 = 14;
q2 = 17;
q3 = 10;
q4 = 14;
finalScore = q1 + q2 + q3 + q4;

System.out.printf("(%d) %s \n", rank, teamName);
System.out.println("-------------------------------");
System.out.println(" Q1  Q2  Q3  Q4  Final");
System.out.println(" --- --- --- --- -----");
System.out.printf(" %-2d  %-2d  %-2d  %-2d  %d \n",q1, q2,q3,q4
                                                  ,finalScore);
System.out.println();
/* end repeatable code */

/* start repeatable code */
// Ohio State
teamName = "Ohio State";
rank = 3;
q1 = 21;
q2 = 10;
q3 = 14;
q4 = 7;
finalScore = q1 + q2 + q3 + q4;

System.out.printf("(%d) %s \n", rank, teamName);
System.out.println("-------------------------------");
System.out.println(" Q1  Q2  Q3  Q4  Final");
System.out.println(" --- --- --- --- -----");
System.out.printf(" %-2d  %-2d  %-2d  %-2d  %d \n",q1, q2,q3,q4
                                                  ,finalScore);
System.out.println();
/* end repeatable code */
```

What potential problems do you see with this approach?

Using functions can help us simplify this code and reduce redundancy.

## Cheat sheets

- [Introduction to Functions](https://www.ask-codey.com/cheat-sheets/introduction-to-functions-an-overview-cheat-sheet)

Source: https://www.ask-codey.com/lessons/introduction-to-functions-practice-the-need-for-functions
