AP Computer Science Principles · Unit 1 lecture · Part 3 of 11 · 7 min

1.2 Program function and purposeWhy it exists, and what it does

Mr. Thompkins
Here's the most useful pair of words in this unit: purpose and function. Students mix them up constantly, and your Create task written response depends on getting them right. Purpose is why. Function is what.

A program, also called software or code, is a collection of program statements that performs a specific task when a computer runs it. Programs are written to solve a problem or to pursue an interest through creative expression, like a game, an animation or a music maker. That reason is the program's purpose. Knowing the purpose guides every decision the developers make, from what to build first to how the screens should look.

A program's function is what it actually does when it runs: what inputs it takes, how it processes them, and what outputs it produces. Developers often describe the function by describing the program's behavior, which is how it acts while it runs, and they usually describe that behavior from the user's side: "you type a stop number and it shows how many minutes until the next bus."

ProgramPurpose (why it exists)Function (what it does)
Tip splitterFriends split a restaurant bill fairlyTakes bill, people, tip %; shows each share
Bus trackerRiders stop waiting in the coldTakes a stop; shows minutes until the next bus
FlashcardsStudents remember vocabularyShows a word; checks the typed answer; repeats misses
Purpose is the problem it solves for people. Function is what it does: inputs in, outputs out.
?Wait, what's the difference between a program and a code segment again?
A program is the whole collection of statements that does a task. A code segment is a piece of a program: a few statements, a loop, or a procedure. A program is made of code segments.

Here is a whole small program, written in the pseudocode the AP exam uses. Don't worry about every symbol yet: Unit 3 teaches the language line by line. For now, read it for its function. It takes three values in, does some arithmetic, and shows one value out.

1PROCEDURE tipShare(bill, people, percent)12{3  tip ← bill * percent / 10024  RETURN((bill + tip) / people)25}6DISPLAY(tipShare(60, 4, 20))3
  1. line 1 Three inputs arrive as parameters
  2. line 3-4 Processing: 12 of tip, then 72 split 4 ways
  3. line 6 Output: the share is shown on the screen
output › 18
The tip splitter. The arrow ← means "store this value in that variable". DISPLAY shows a value on the screen.
?Wait, what does PROCEDURE mean?
A procedure is a named block of code that does one job. The names in parentheses, like bill and people, are its parameters: the inputs it works with. A line like tipShare(60, 4, 20) runs it with those values. RETURN sends the answer back. You'll learn procedures fully in Unit 3.
inputsbill: 60people: 4percent: 20tipShareprocessingshows 18output
Input, processing, output. The three values go in, the procedure processes them, and 18 comes out.

One more expectation from the course: a program needs to work for a variety of inputs and situations, not just the one the developer tried first. The tip splitter works for 60 dollars and 4 people. Does it work for 1 person? For a 0 percent tip? For 0 people? Thinking about all of those is part of designing the function, and it leads straight into testing in Topic 1.4.

Pause and think
What does tipShare(100, 5, 15) display? Work it out before you check.
I thought about it. Show me
15 percent of 100 is a tip of 15, so the total is 115, and 115 split 5 ways is 23. It displays 23.
Purpose or function?
A student describes her Create task: "My program helps middle school students practice multiplication facts. It shows two random numbers from 1 to 12, the student types the product, and the program says Correct or Try again and keeps score." Identify the purpose, the inputs, and the outputs.
  1. Purpose: the problem it solves for people. Here, helping middle school students practice multiplication facts.
    Why?
    Purpose answers "why does this exist and for whom?" It is about people, not code.
  2. Inputs: the product the student types. (The random numbers are produced by the program itself, so they are not user input.)
    Why?
    An input is data sent to the program for processing. The typed answer comes from the user.
  3. Outputs: the two numbers shown, the message Correct or Try again, and the score.
    Why?
    An output is any data the program sends to a device, here to the screen. Notice the message depends on the input: outputs are usually based on input.
  4. Function, in one sentence: it shows a problem, checks the typed answer against the product, and reports the result and running score.

Answer: Purpose: helping middle school students practice multiplication facts. Input: the typed product. Outputs: the two numbers, Correct or Try again, and the score.

Stuck? Get a hint
  1. Purpose is about people and a problem. Find the "helps ... to ..." part.
  2. An input is something sent into the program. What does the student send?
  3. An output is anything the program shows, plays or sends out.
Check yourself
Which statement describes the purpose of a program, rather than its function?
  1. AIt reads the temperature sensor every minute and stores the reading.
  2. BIt helps gardeners know when their plants need water.
  3. CIt displays a red icon when the soil moisture is below 30 percent.
  4. DIt sends a text message after three dry readings in a row.
Show the answer

B. Purpose is why the program exists for people: helping gardeners know when to water. The other three describe what the program does with its inputs and outputs, which is its function. The trap is that function statements sound more "technical", so they feel more important.

Exam tip from Mr. T

When a written-response prompt asks about purpose, name a specific group of people and the problem the program solves for them. "It helps anyone" earns nothing. "It helps high school runners track weekly mileage" does.

  • Purpose: the problem the program solves, or the interest it pursues, for people.
  • Function: what it does when it runs. Inputs, processing, outputs. Its behavior is usually described from the user's side.
  • A program is a collection of statements that performs a task. A code segment is part of a program.
  • A program should work for a variety of inputs and situations, not only the obvious one.