USE INPUT AND OUTPUT

Previous lessons have glossed over something important -- how to get information into a program, and how to get the results out. This area of programming is called IO (Input/Output). In the previous example, you used a simple statement called print to tell the computer to print text to the screen.

As with any statement, different languages have a different name for the print statement, so don't be surprised when you see another name for it later.

There's an obvious counterpart to the print statement, which is the input statement (also known by other names such as read in other languages). You use this statement to tell the computer to get some data from the user. For example, to ask the user's age, you might say:

print "How old are you?"
input (age)

In this case, you tell the computer to print the message, "How old are you?" and then read in something the user types, which you store in a variable called age . The convention for an input command is to accept characters from the keyboard until the Enter key is pressed, at which point all of the accumulated characters are interpreted as the age variable.

To see subprograms, input, and output at work, write a simple program in pseudocode that uses the average3 function along with some user input and output. It'll ask the user for three numbers, calculate the average, and print it.

real function average3 (real number1, real number2,
            real number3)
  average3 = (number1 + number2 + number3) / 3
end function
program do_average
  print "This program will ask for three numbers,
      and calculate the average."
  print "Enter the first number:"
  input (number1)
  print "Enter the second number:"
  input (number2)
  print "Enter the third number:"
  input (number3)
  the_avg = average3(number1, number2, number3)
  print "The average is :", the_avg
end program

In a real language, there would be statements to declare your variables, but I am skipping that detail in this pseudoprogram, for simplicity's sake.

When you run the program, it prints the first messages asking you for a number. When you type in a number, it stores that number as number1 . It then asks in sequence for two more numbers, and stores them as number2 and number3 .

The program then calls the function average3, passing it the parameters stored in number1 , number2 , and number3 . Because average3 is a function, it returns a value that you store in another variable called the_avg . You could state the effect of the function call as, "set the variable the_avg equal to the value the function average3 returns when given number1 , number2 , and number3" . Finally, the program prints the value of the variable the_avg and ends.

Other Kinds of Input and Output

Naturally there are other kinds of input and output besides printing to the screen and accepting keyboard input. For example, you can write a program that reads a mailing list from a file and prints Christmas card envelopes on a printer; however, all forms of input and output have universal requirements. Your program must describe output in terms of where it is going and exactly what data is to be output. Code describing input must state where it is coming from and the variables where the input is to be stored.

Twenty years ago, a discussion of input and output would have stopped here. But modern programmers have to deal with GUIs (graphic user interfaces) as found in Web browsers and the Windows operating system.