A program's purpose is why it exists: the problem it solves or the interest it serves. Its function is what it does when it runs: the inputs it takes and the outputs it produces. You describe what a code segment does either by tracing its code or by testing its behavior on inputs you choose.
Quick check
What does DISPLAY(3 + 4 * 2) display?Show answerHide answer
It displays 11. Multiplication comes before addition, so 4 * 2 is 8, and 3 + 8 is 11.
A music app plays a song when you tap its title. Which part is the input and which is the output?Show answerHide answer
The tap is the input; the sound is the output.
Know these already? Jump to the practice questions
What you'll be able to do
- Distinguish a program's purpose from its function.
- Identify a program's inputs, including events, and its outputs.
- Describe what a code segment does by tracing it.
- Determine what a code segment does from its results on chosen inputs.
Key terms
Purpose
The problem a program is meant to solve, or the interest it serves.
- In plain English
- Why the program exists.
- Don't write
- "Its purpose is to display the total." That is a function.
Function
What a program or code segment does when it runs: how it turns inputs into outputs.
- In plain English
- What the program actually does.
- Don't write
- "Its function is to help people save money." That is a purpose.
Input and output
Input is data a program receives, such as typed text, a click or a sensor reading; output is data it sends out, such as screen text or a sound.
- In plain English
- What goes in and what comes out.
- Don't write
- "The screen is an input because the user looks at it."
Event
An action, such as a click or a key press, that causes part of a program to run.
- In plain English
- Something that triggers code.
- Don't write
- "An event is any line of code."
What is the difference between purpose and function?
Purpose answers "why was this made?"; function answers "what does it do when it runs?"
| A step-tracker app | |
|---|---|
| Purpose | Encourage its users to walk more |
| Function | Reads the motion sensor, adds up steps, shows the daily total and a message at the goal |
Some programs solve a problem, such as tracking a budget; others serve an interest or creativity, such as a game. Both are purposes. The purpose guides the design: each function should serve it. A useful test: a purpose still makes sense if the code were rewritten; a function describes what this code actually does.
Check yourself
Show answerHide answer
Its function: it says what the app does when it runs, not why it was made.
What counts as an input and an output?
Inputs reach a program from a user, a device or another program: typed text, a click, a key press, a camera or motion-sensor reading, or a file. Outputs go the other way: screen text and images, sound, a vibration, a saved file, or data for another program.
INPUT() accepts a value from the user and DISPLAY(expression) shows a value (on the exam reference sheet):
n ← INPUT()
DISPLAY(n * 2)
Entering 6 gives 12; entering 10 gives 20. The same code gives different outputs for different inputs, so a description of a function names its inputs.
Check yourself
Show answerHide answer
An input. Switching the heater on is an output.
How do events decide which code runs?
In an event-driven program, blocks of code are linked to events: a button click, a key press, a timer, a sensor change. A block runs when its event happens and waits otherwise, so the order in which code runs depends on what the user does, not only on where the code sits.
The same event can happen many times, and its code runs each time. Variables keep their values between events, so one click can change what the next click shows.
Key idea
Events are inputs: they decide which code runs and when.
Check yourself
Show answerHide answer
- The code runs once per press.
How do you describe what a code segment does?
There are two routes:
- From the code: trace it on a sample input, then state the pattern in one sentence.
- From its behavior: run it on inputs you choose and look for the pattern in the outputs. Include inputs that could break your guess: an empty list, equal values, a boundary value.
A good description names the input, output and condition: "returns the sum of the even numbers in the list". A line-by-line retelling ("sets result to 0, then loops…") describes steps, not the result.
Examiner's eye
Marks are lost when a description retells each line instead of saying what the code does with its input.
Check yourself
Show answerHide answer
The second, because it names the output and the condition.
Worked example 1: Purpose or function?
Question: A school app lets students book help from peer tutors. Which statement describes the app's purpose?
- A) It shows a calendar and saves a booking when a student taps an open slot.
- B) It sends a reminder one hour before each session.
- C) It helps struggling students get support from classmates at times that suit them.
- D) It stores each tutor's free times in a list.
What tells you the method
"Purpose" asks why the app exists, so remove every option that says what the code does.
Step 1: Sort the options.
A and B describe behavior; D describes how data is stored.
WhyA function statement names actions, inputs or outputs.
Step 2: Check the one left.
C names the users and the problem solved.
WhyA purpose would survive a rewrite of the code.
Check it
Only C answers "why was this app built?"
Answer
C
Worked example 2: Trace, then describe
Question: What does mystery([4, 7, 10, 3]) return? Then describe what the procedure does.
PROCEDURE mystery(nums)
{
result ← 0
FOR EACH n IN nums
{
IF(n MOD 2 = 0)
{
result ← result + n
}
}
RETURN(result)
}
What tells you the method
A call with a given list, so trace it pass by pass.
Step 1: Trace the loop.
n MOD 2is the remainder whennis divided by 2 (on the exam reference sheet), so it is 0 for even numbers.Pass nn MOD 2 = 0resultstart 0 1 4 true 4 2 7 false 4 3 10 true 14 4 3 false 14 Whyresultchanges only when the condition is true.Step 2: State the pattern.
Only the even values, 4 and 10, were added: it returns the sum of the even numbers in
nums.WhyA description names the input, the output and the condition.
Check it
Test the description on [1, 3], which has no even numbers: tracing gives 0, as predicted.
Answer
14
Worked example 3: Match code to behavior
Question: A procedure pick(a, b) gave these results. Which body could it have?
| Call | Returned |
|---|---|
pick(3, 5) | 5 |
pick(9, 2) | 9 |
pick(4, 4) | 4 |
- A)
RETURN(b) - B)
IF(a > b) { RETURN(a) } ELSE { RETURN(b) } - C)
IF(a < b) { RETURN(a) } ELSE { RETURN(b) } - D)
RETURN(a)
What tells you the method
Behavior given as a table, so trace every option on every row.
Step 1: Trace each option.
Option (3, 5)(9, 2)(4, 4)A 5 2 4 B 5 9 4 C 3 2 4 D 3 9 4 WhyOne row that disagrees rules out an option.
Step 2: Compare with the results.
Only B matches all three rows: it returns the larger value.
WhyA and D each match two rows, so checking one row is not enough.
Check it
On the tie, B's condition 4 > 4 is false, so the ELSE returns b, which is 4, as recorded.
Answer
B
Your turn
Question: When an app starts, total ← 0 runs once. Then:
total ← total + 5
DISPLAY(total)
runs each time the user clicks Add, and
total ← 0
DISPLAY(total)
runs each time the user clicks Reset. The user clicks Add, Add, Reset, Add. What is displayed?
Step 1: First click (Add).
totalbecomes 5, and 5 is displayed.Step 2: Second click (Add).
totalbecomes 10, and 10 is displayed.Step 3: Your go: handle Reset, then the last Add.
Show the full solutionHide the solution
Step 3. Reset sets total to 0 and displays 0. The last Add makes total 5 and displays 5, on the same line.
Answer: 5 10 0 5
Traps
Trap
A program's purpose is a summary of what its code does.
It seems right because both describe the program. But the purpose is why it exists; a summary of behavior is its function.
"It converts a recipe for 4 people into a recipe for any number." Purpose or function?
Function: it describes what the program does.
Trap
Code in an event-driven program runs from top to bottom, once.
It seems right because simple programs run that way. But each event's code runs whenever its event happens, in the order the user triggers it.
A Reset button's code sits at the top of a program. Does it run first?
No. It runs only when the user clicks Reset.
What's examined
On the AP® Computer Science Principles exam, this belongs to Big Idea 1: Creative Development. Multiple-choice questions ask you to tell purpose from function, identify inputs and outputs, and match a description or code segment to given behavior. The written-response questions ask about your own Create program, so practice describing code in one specific sentence.
Practice questions
- Recall A fitness app reads a motion sensor, shows a step total and plays a sound at the goal. Which is an input?
- A) The step total on the screen
- B) The sound played at the goal
- C) The motion sensor's readings
- D) The list storing each day's total
Show answer
Answer: C. The sensor readings come into the app from a device.
Why the others are wrong:
- A is an output shown to the user.
- B is an output the user hears.
- D is data the app keeps inside itself, not data coming in.
- Recall Name the kind of action, such as a click or a key press, that causes part of a program to run.
Show answer
Answer: Event
Marking points:
- An event triggers the code linked to it, each time it happens.
- Apply What is displayed when this code segment runs?
nums ← [2, 5, 8]
FOR EACH n IN nums
{
IF(n > 4)
{
DISPLAY(n * 10)
}
ELSE
{
DISPLAY(n)
}
}
Show answer
Answer: 2 50 80
Marking points:
- Pass 1:
2 > 4is false, so 2 is displayed. - Passes 2 and 3: 5 and 8 are greater than 4, so 50 and 80 are displayed.
- Apply Which statement describes the function of a recipe-scaling app rather than its purpose?
- A) It multiplies each amount by the servings the user enters and displays the results.
- B) It helps cooks make the right amount of food for any group.
- C) It was built to reduce food waste.
- D) It is meant for people who cook for large families.
Show answer
Answer: A. It names an input, what the app does with it and the output.
Why the others are wrong:
- B states the problem the app solves, which is its purpose.
- C gives the reason it was built, also a purpose.
- D names the intended users, not what the app does.
- Exam-style Which best describes what this code segment does with the list
temps?
count ← 0
FOR EACH t IN temps
{
IF(t < 0)
{
count ← count + 1
}
}
DISPLAY(count)
- A) It displays every temperature below 0.
- B) It displays the lowest temperature in
temps. - C) It displays how many temperatures are 0 or below.
- D) It displays how many temperatures are below 0.
Show answer
Answer: D. count goes up once for each t less than 0, and only count is displayed.
Why the others are wrong:
- A:
DISPLAYruns once, after the loop, so it shows a count, not the temperatures. - B: nothing tracks the smallest value.
- C:
t < 0is false whentis 0, so 0 is not counted.
- Exam-style A map app zooms in when the user presses + and moves when the user drags it. Which statement is accurate?
- A) Its statements run once each, top to bottom.
- B) Which code runs, and in what order, depends on the events the user triggers.
- C) The key press is an output, because it changes the map.
- D) The map image is an input, because the user looks at it.
Show answer
Answer: B. Each event's code runs when that event happens.
Why the others are wrong:
- A describes a program with no events.
- C: the key press comes from the user, so it is an input; the zoomed map is the output.
- D: the map image is shown to the user, so it is an output.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- Purpose is why a program exists; function is what it does when it runs.
- Inputs come in from users, devices or other programs; outputs go out to them.
- Events such as clicks and key presses decide which code runs and when.
- Describe a code segment by its input, output and condition, not line by line.
- Test a description on inputs that could break it, such as a tie or an empty list.