SUBPROGRAMS MAKE PROGRAMMING EASIER

In the previous lesson, you learned about logical control statements and loops that handle repetitive tasks. In this lesson, you learn about the use of subprograms and how they facilitate breaking complex programming problems into simpler parts. You also learn about a new topic, what programmers call IO -- short for Input and Output .

A subprogram encapsulates a programming chore in a small chunk of code that can be repeatedly used by other parts of your program. Without subprograms, tackling large problems would be impossible.

For example, suppose you have a chess program that has to draw the game board onscreen. It would be tedious to write the instructions to draw 64 individual squares with or without the chess piece images. Instead, you can create a subprogram to draw a square at a programmable location with a programmable image. Wherever your program has to repeat certain steps, you have a candidate for a subprogram.

Organizing all but the simplest programs in terms of subprograms results in code that's easier to understand and to maintain. It's also easier to improve because if you discover a faster way to draw images, for example, you only have to change one piece of code.

Different programming languages use different terms when talking about subprograms. Some commonly used terms are subroutine , procedure , method , and function . When you write the code to run a subprogram, you call the subprogram, or invoke it. Programming languages make a distinction between functions, which return a value to the calling program, and subroutines or procedures, which do not.

Most programming languages have bundled or included subprograms or functions that allow you to do certain kinds of work (like count the number of characters in a string, open files, and so on).

Define a Function

For this example function, suppose you have a program that frequently needs the average of three numbers. You can write a function that takes three numbers and returns the average. In most languages, you would then call this function by saying something similar to:

avg = average3(var1, var2, var3)

This tells the computer to execute the instructions in the average3 function with the values in the variables var1 , var2 , and var3 as input and store the resulting value in the variable called avg .

Now to tell the computer what the average3 function is. You do that by defining the function. In the function definition, you tell the computer what type of value the function is going to return, what information you have to give the function (in this case, the three numbers), and what the function should do.

In pseudocode, this average3 function definition looks similar to the following:

real function average3(real number1, real number2, 
            real number3)
  average3 = (number1 + number2 + number3) / 3
end function

In the previous pseudocode, you define the function by stating what type of value it returns and what types of values it needs to be given when you call it. In this case, you told the computer you were defining a function called average3 that would require three real values and would return a real value. The function adds the three numbers together, divides by 3, and assigns the result to a variable that has the same name as the function.

This convention of designating the value returned with a variable that has the function's name is used in QBasic and related languages. In other languages such as Java, a return command is used in code that looks similar to the following:

return (number1 + number2 + number3 ) / 3 ;

The values (variables) that you give any type of subprogram when you call it have a special name called parameters . In the previous example, number1 , number2 , and number3 are the parameters.

Define Subroutines

Subroutines are called similar to functions, but your program does'nt have to catch a returned value. Subroutines are often used to perform operations such as writing text to the screen or to a file.

The following is an example of a subroutine that calculates an age and prints it.

subroutine show_age(integer current_year, 
          integer year_born)
  integer new_age;
  new_age = current_year - year_born
  print "Your age is:" new_age
end subroutine show_age

You call your subroutine and give it the current year and the year in which someone was born. It does the subtraction and prints the value to the screen. Note that the variable new_age that is defined and used inside the subroutine can only be used inside show_age . It's called a local variable.

Some languages make no distinction between subroutines and functions. In JavaScript, for example, you can define a function as having a returned value or having no returned value.