Input-processing-output (IPO) is the most common pattern. Here it is in action:
'Declare variables
Dim amount As Single
Dim tip As Single
Dim total As Single
'Get data from the worksheet
amount = Cells(1, 2)
'Compute results
tip = amount * 0.15
total = amount + tip
'Put results into worksheet
Cells(2, 2) = tip
Cells(3, 2) = total
There are three chunks here, forgetting declaring variables for now. In this and the other programs you've seen, input is always the same: from the worksheet. But input can come from other places. In fact, input from the worksheet is itself a pattern.
Input patterns
Here are some other input patterns.
Input from the keyboard
You can ask the user to type something at the keyboard. Look at line 6 in this example:
'Declare variables
Dim amount As Single
Dim tip As Single
Dim total As Single
'Get data from the keyboard
amount = InputBox("How much was the bill?")
'Compute results
tip = amount * 0.15
total = amount + tip
'Put results into worksheet
Cells(2, 2) = tip
Cells(3, 2) = total
Input from a file
Maybe you have a file that was downloaded from the Internet, with the data you need. You can input data from it.
More on files later in the course.
Input is abstract
Here's IPO again.
The "input" step doesn't tell you exactly how to code something. It's just: Get input from somewhere, dude.
Input is an abstract step. To implement it, you need to use a concrete pattern like "Input from worksheet cells," "Input from keyboard," or "Input from file." These concrete patterns nest inside IPO, which is completely abstract.
Output patterns
As for input, so for output. The O in IPO is abstract. You need to pick a concrete pattern when you write actual code.
Output to a worksheet cell
You know this one.
Output to the screen
Output to a file, maybe a Web page
Later, you'll learn how to write a program that creates a Web page.
It's all good
A benefit of thunking is that you can switch out the patterns you use. Does your program need to get input from a different place? Change the I, and leave P and O alone.
Exercises
Exercise: Compute tip again
Redo the compute tip program, so that it gets input from a dialog box, and shows output in a dialog box..
Upload your Excel file.
(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)
Exercise: Favorite animal again
Redo the favorite animal program with different I/O. That's geek for input/output. Get data from the keyboard, and show output to the screen using message boxes. Also, add two more animals.
Upload your Excel file.
(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)
Summary
Input in IPO is an abstract step. To implement it, you need to use a concrete pattern like "Input from worksheet cells," "Input from keyboard," or "Input from file." These concrete patterns nest inside IPO.
The O in IPO is abstract as well. You need to pick a concrete pattern when you write actual code.
A benefit of thunking is that you can switch out the patterns you use. Just change the code you need to, and leave the rest alone.