Skip to main content

Unit 1 · Topic 1.2

1.2 Program Function and Purpose

A program is a set of instructions that does a job when a computer runs it. This topic separates a program's purpose (why it exists) from its function (what it does when it runs), and shows how inputs, outputs and events drive what a program does.

Key terms

  • purpose
  • behavior (function)
  • input
  • output
  • event
  • code segment

Purpose versus function

The purpose of a program is the reason it exists: the problem it solves or the creative interest it serves. A budgeting app's purpose is to help people keep track of their money. A drawing app's purpose is to let people make art.

The function (or behavior) of a program is what it actually does when it runs, usually described by how a user interacts with it. The budgeting app's function might be: the user types in each purchase, picks a category, and the app shows a running total for each category and warns when one goes over its limit.

Knowing the purpose helps developers make better choices. If you know a timer app is for people cooking with messy hands, you might add large buttons or voice control.

You can describe a program at two levels. Broadly, you say what it does ("it sorts your playlist by mood"). In more detail, you say what it does and how the code makes that happen ("it gives each song a mood score from its tempo, then orders the list by that score").

Programs and code segments

A program is a collection of statements that does a specific task when a computer runs it. Another word for programs is software.

A code segment is a smaller group of statements that is part of a program, like a loop, an IF block or a procedure. Exam questions often show you a code segment and ask what it does.

A program has to work for many different inputs and situations, not just the one the programmer had in mind. A grade calculator that crashes when a student has no grades yet doesn't fully work.

Inputs and outputs

Input is data sent to a program for it to process. It can come from a user (typing, tapping, speaking), from a device such as a camera, microphone or temperature sensor, or from another program, file or online data feed. Input can be text, sound, images or touch.

Output is data a program sends to a device: text on a screen, a sound, an image, a vibration or a file. Output usually depends on the input, and it can also depend on the program's prior state, meaning values it has stored, like a score or a saved setting.

ProgramExample inputExample output
Music appTapping a song titleThe song plays
Fitness trackerSteps counted by a motion sensorA daily step total on the screen
GameArrow keys pressedThe character moves; the score updates
Weather appA data feed from a weather serviceA forecast and an icon

Events and event-driven programs

An event is an action that supplies input to a program and can change what it runs next: a key press, a mouse click, a screen tap, a timer going off, or the program starting.

In an event-driven program, code doesn't just run top to bottom. Instead, chunks of code wait until their event happens, then run. A game might have one block that runs when the left arrow is pressed and another that runs when the space bar is pressed. Which code runs, and in what order, depends on what the user does.

Most apps you use are event-driven. Code.org's App Lab, Scratch and web pages all use events like onEvent or "when this sprite clicked."

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Identifying purpose, input and output

    A school builds a lunch-line app. Students scan their ID card at a kiosk, the app looks up their account, subtracts the cost of the meal and shows the new balance. If the balance falls below $5, the app sends a text message to the student's parent. Identify the program's purpose, two inputs and two outputs.

    Show the solution
    1. Step 1: Purpose: ask why the program exists. It makes paying for lunch quick and helps families keep accounts funded.
    2. Step 2: Inputs: look for data coming into the program. The scanned ID card (from a card reader) and the cost of the meal are inputs. The stored account balance is also data the program uses, but it's prior state rather than new input.
    3. Step 3: Outputs: look for data the program sends out. The new balance shown on the kiosk screen, and the text message sent to a parent.
    4. Step 4: Notice that the text message depends on both the input (this meal's cost) and the prior state (the old balance).

    Answer: Purpose: fast lunch payment and keeping accounts funded. Inputs: the scanned ID and the meal cost. Outputs: the balance on screen and the low-balance text to a parent.

Common mistakes

  • Confusing purpose with function. "It lets users enter purchases and shows totals" is function; "it helps people manage their money" is purpose.
  • Forgetting that input doesn't have to come from a person typing. Sensors, files, other programs and events all count.
  • Thinking event-driven code runs in the order it's written. It runs when its event happens.

On the exam

  • Written Response 1 on the exam asks about your own Create task program, such as who it's for, what its inputs and outputs are, or how its design serves its purpose. Practice describing your program's purpose in one clear sentence and its function in two or three.
  • Multiple-choice questions may describe an app and ask which item is an input, an output or the program's purpose.

Connected topics

Videos

Check yourself

4 questions on 1.2 Program Function and Purpose. Pick an answer to see if you got it, and why.

Question 1 of 4

A developer describes a new program in four ways. Which description states the program's purpose rather than its function?

Question 2 of 4

In a drawing app, a block of code that clears the canvas runs only when the user clicks the "Clear" button. Which of the following best describes how this code is run?

Question 3 of 4

Which two of the following are inputs to a fitness-tracking watch program? Select two answers.

Select two answers. 0 of 2 chosen

Question 4 of 4

A music app shows a "Play" button when no song is playing and a "Pause" button when a song is playing. Which statement best explains why the button the app shows can change even when the user has not touched the screen?

0 of 4 answered