Program Function and Purpose
Why a program exists, what it does when it runs, and the inputs, events, and outputs that connect it to people and devices.
1Purpose
The purpose of a computing innovation is why it exists. Purposes come in two kinds: to solve a problem (an app that warns students with allergies about lunch ingredients) or to pursue an interest through creative expression (a drawing app, a music maker, a game). A creative purpose is just as real as a problem-solving one.
Knowing the purpose helps developers build the innovation better: every feature can be checked against it. If the purpose of a bus app is to help riders avoid waiting at the stop, a live arrival time matters far more than a page of company history.
State a purpose from the user’s side, with words like help, let, or allow followed by the user’s goal.
2Function
A program is a collection of statements that performs a specific task when a computer runs it; another word for a program is software. A code segment is a collection of statements that is part of a program. The function of a program or code segment is what it does when it runs. Its behavior is how it acts during execution, often described by how a user interacts with it.
A function can be described broadly, by what the program does, or in more detail, by what it does and how its statements do it. Here is a segment from a bill-splitting app:
bill ← INPUT() people ← INPUT() tip ← bill * 20 / 100 total ← bill + tip share ← total / people DISPLAY(share)
| Level | Description |
|---|---|
| Broad | It splits a restaurant bill, plus a 20% tip, evenly among the people at the table. |
| Detailed | It reads the bill and the number of people, calculates the tip as 20% of the bill, adds the tip to the bill, divides the total by the number of people, and displays each person’s share. |
Both are descriptions of function. “It helps friends pay fairly” is the purpose.
3Working for many inputs
A program needs to work for a variety of inputs and situations, not only the one its author had in mind. Running the bill splitter on several inputs:
| bill | people | Displayed |
|---|---|---|
| 50 | 2 | 30 |
| 45 | 3 | 18 |
| 25 | 4 | 7.5 |
| 40 | 0 | the program stops with an error (dividing by zero) |
The first three work. With 0 people, the segment tries to divide by zero and the program stops. A complete description of a program’s function covers every path its input can take, including empty and unexpected input, and a good program checks for those cases before they cause trouble.
4Inputs and events
Program inputs are data sent to a computer for a program to process. Input can be tactile (a tap, a button press), audio (a spoken command), visual (a camera image, a scanned barcode), or text (typed characters). It can come from a user or from another program: a weather app receives forecasts from a weather service’s program. Inputs usually affect the output the program produces; that is why the bill splitter shows a different share for each bill.
An event is an action that supplies input data to a program. Events are generated when a key is pressed, a mouse is clicked, a program is started, or any other defined action happens that affects which statements run next. Tapping a map is the event; where you tapped is the input data it supplies.
Most code you trace runs sequentially: statement 1, then 2, then 3. In event-driven programming, statements run when an event triggers them instead. Most apps you use work this way: blocks of code wait until a tap, a key press, or a sensor reading sets them off.
5Outputs
Program outputs are any data sent from a program to a device. Like input, output can be tactile (a phone vibrating), audio (an alarm), visual (a map, a color change), or text. A value the program calculates and stores is not output until it is sent to a device: total ← bill + tip only changes memory; DISPLAY(share) sends a value to the screen.
Output is usually based on the program’s input or its prior state, meaning values it already has stored, such as a counter or a saved setting. In this segment, best holds the highest score so far:
score ← INPUT()
IF (score > best)
{
best ← score
DISPLAY("New best!")
}
ELSE
{
DISPLAY("Best is still")
DISPLAY(best)
}
| Run | best before | Input | Displayed | best after |
|---|---|---|---|---|
| First | 80 | 85 | New best! | 85 |
| Second | 85 | 85 | Best is still 85 | 85 |
The same input, 85, produces different output because the stored value of best changed in between.
Purpose is why an innovation exists: to solve a problem or for creative expression. Function is what a program does when it runs, described broadly or in detail, and it must hold up for many inputs. Inputs (tactile, audio, visual, text; from users, events, or other programs) go in; outputs are data sent to a device, based on input or stored state.
6Worked examples
The fitness tracker
Label each statement about a fitness tracker as purpose or function. (a) It helps users reach a daily activity goal. (b) It counts steps from its motion sensor and updates a daily total. (c) It vibrates when the user has been still for an hour.
- 1(a) names the user’s goal: purpose.
- 2(b) describes what happens during execution: function.
- 3(c) also describes behavior while running, with a tactile output: function.
The checkout scanner
At a store’s self-checkout, a customer scans a barcode, says “yes” to a bag, and taps Pay. The screen shows the total and the machine beeps. List the inputs and outputs and their forms.
- 1Inputs: the barcode image (visual), the spoken “yes” (audio), the tap on Pay (tactile; the tap is an event that supplies input).
- 2The price of each item may also come from the store’s inventory program: input from another program.
- 3Outputs: the total on the screen (text and visual) and the beep (audio).
Hot days
The list temps holds 28, 31, 33, 29, 35, 30, 27. Describe what this segment does broadly and in detail, and give its output.
count ← 0
FOR EACH t IN temps
{
IF (t > 30)
{
count ← count + 1
}
}
DISPLAY(count)
- 1Broad: it counts how many temperatures in the list are above 30.
- 2Detailed: it sets
countto 0, looks at each temperature in turn, adds 1 tocountwhen the temperature is greater than 30, and displayscountat the end. - 3Values above 30: 31, 33, 35. The 30 is not counted, because 30 > 30 is false.
- 4For an empty list the loop body never runs and it displays
0, which is still correct.
3.Which lines send data out?
Which statements produce output, and what is displayed when the input is 34?
temp ← INPUT()
warnings ← 0
DISPLAY(temp)
IF (temp > 30)
{
DISPLAY("Heat warning")
warnings ← warnings + 1
}
- 1
temp ← INPUT()brings data in: input. - 2
warnings ← 0andwarnings ← warnings + 1only change stored values. - 3The two
DISPLAYstatements send data to the screen: those are the outputs. - 4With 34,
34 > 30is true, so both display statements run.
34 Heat warning. With 22 only 22 would be displayed.7Common mistakes
“It helps students study” is a purpose. A function says what the program does: reads, compares, calculates, displays.
Taps, voice, camera images, sensor readings, and data from other programs are all input.
A value becomes output only when it is sent to a device such as a screen or speaker.
A function description that ignores empty or unusual input is incomplete. Say what happens on every path.
“The segment reads the bill and the number of people, adds a 20% tip, divides the total by the number of people, and displays each person’s share. For a bill of 50 and 2 people it displays 30.”
Say what it does with its inputs and what it outputs; add a sample input and output when it helps.