# Motivation

Programs tend to have many lines of code and parts of it often implement complex operations. In addition, a task can be performed several times in different parts of the program. Consequently, similar pieces of code performing conceptually the same operation appear repeatedly in the program.

Example: Write a program that reads course grades, for two classes of students (class A and class B). For each of the classes, the program should then display the number of grades that fall in each of the following $11$ clusters (or intervals): $[0,9]$, $[10,19]$, $[20,29]$, $\cdots$, $[90,99]$, $[100,100]$. The grades are integers between zero and $100$. The user ends each list of grades with an integer outside the range $[0,100]$ (e.g. $-1$ or $200$). An example of the program execution is given next.

A program solving this problem is shown below. Take a first look at it. It’s somewhat lengthy and complicated, isn’t it? Notice how the code from line $27$ to $41$ is similar to the code in lines $44$ to $58$. Code from line $22$ to $24$ is also almost a copy of the code in lines $12$ to $14$.

Long programs, possibly with blocks of similar code repeated several times, are difficult to understand, prone to bugs, and costly to maintain when small changes are required. Functions help the programmer to keep the code more organized and, therefore, easy to read. Functions are small reusable building blocks of programs and their use has the following advantages:

Consider again the main function of the example above and compare it with the main of the program given below (which solves the same problem as in example $1$). The program below is structured with the help of two functions, read_grades and display_cluster_counters. The details of these functions are less important now and they will be discussed in section Functions and vectors.
The important at this point is that you notice how the code became simpler to read and repeated blocks of similar code have disappeared from the program. Note that a function, such as display_cluster_counters, can be used in more than one place of the program (see lines $22$ and $26$).