impera Open Impera

AP Computer Science Principles · Creative development

Program function and purpose

1Purpose and function

  • A program's purpose is the problem it solves or the creative goal it serves: why it exists.
  • Its function is what it does when it runs: how it behaves for given inputs.
Example

a step-counting app's purpose is to encourage exercise; its function is to read motion data, count steps, and display the total and progress toward a daily goal.

A computing innovation includes a program as an integral part of its function: apps, smart devices, self-driving cars, social media platforms.

Check yourself

Which describes the purpose of a weather app?

  • To download data from a server every hour

  • To help people plan for the weather

  • To store temperatures in a list

  • To display icons on a screen

Show the answer

To help people plan for the weather

Purpose is the why.

2Inputs, outputs and events

Programs take input and produce output.

  • Input can come from the user (typing, tapping, speaking), from sensors (GPS, camera, accelerometer), from files or from other programs.
  • Output can be visual (text, images), audio, tactile (vibration) or data sent to another device.

Many programs are event-driven: code runs in response to events such as a button click, a key press or a timer finishing. Event handlers wait until their event happens, so the order of execution depends on the user.

Worked example

a quiz app.

  1. Event: user taps an answer button.
  2. Input: which option was chosen.
  3. Processing: compare with the correct answer; update the score.
  4. Output: show "Correct!" and the new score.

Event-driven program: an event triggers input, processing and output

Check yourself

In an event-driven program, code runs:

  • When a specific event, such as a click, occurs

  • Only when the program is compiled

  • At random times

  • Only once from top to bottom

Show the answer

When a specific event, such as a click, occurs

Event handlers.

3Program behaviour

A program's behaviour is how it acts during execution, including how users interact with it. Describing behaviour means describing what happens for different inputs, including unusual ones.

Code segments are collections of program statements that are part of a larger program. Programs can be described in plain language, in diagrams, or in code.

Check yourself

Which is an example of input from a sensor?

  • Playing a notification sound

  • Showing a map on screen

  • Printing a receipt

  • A phone detecting its location with GPS

Show the answer

A phone detecting its location with GPS

Sensor data in.

4Documentation and comments

Program documentation is a written description of how a program, or a part of it, works. Comments are documentation inside the code, ignored by the computer.

Documentation helps:

  • the original programmer remember their reasoning;
  • others understand, use and modify the code;
  • teams collaborate.

It should be written during development, not just at the end.

Check yourself

Comments in program code are:

  • Required for the program to compile and run

  • Executed as instructions when the program runs

  • Ignored when the program runs but help people understand it

  • Read only by the compiler, never by people

Show the answer

Ignored when the program runs but help people understand it

For humans.

5Going deeper: describing a program precisely

Purpose and function are different.

  • Purpose: why the program exists: the problem it solves or the creative interest it pursues. "To help users track how much water they drink."
  • Function: what the program does: how it behaves when it runs. "The user enters each drink's volume; the program adds it to a running total and displays the total and the amount left to reach the daily goal."

A good description of function mentions input, what the program does with it, and output.

Input: data sent to the program.

  • From a user: typing, clicks, taps, voice, gestures.
  • From a device or sensor: camera, microphone, GPS, accelerometer, temperature sensor.
  • From another program or the internet: a file, a database, an online data feed.

Input can be text, numbers, images, sound or touch.

Output: data sent from the program to a device or user: text or graphics on a screen, sound, vibration, a file, a signal to a motor.

Output usually depends on the input and on the program's earlier state.

Events: an event is an action that supplies input to a program: a key press, a mouse click, a screen tap, a timer reaching zero, a message arriving. In event-driven programming, code is not run in one fixed order from top to bottom; segments run when their event occurs. Most apps and games work this way.

Program behavior: how a program functions during execution, often described by how a user interacts with it.

A program, a code segment, a statement

  • A program is a collection of statements that performs a task.
  • A code segment is a part of a program.
  • A code statement expresses a single action.

Describing a program well

Worked example (weak and strong): a quiz app.

  1. Weak purpose: "The purpose is to ask questions." (That is the function.)
  2. Strong purpose: "To help students revise vocabulary for a language class."
  3. Weak function: "It is a quiz."
  4. Strong function: "The program shows a word; the user types a translation (input). The program compares it with the stored answer, displays 'Correct' or the right answer (output), and updates the score. After ten words it displays the final score."

Worked example (identify input and output): a navigation app.

  1. Inputs: the destination typed or spoken by the user; the phone's position from GPS; live traffic data from the internet.
  2. Outputs: a map with a highlighted route; spoken directions; an estimated arrival time.

Worked example (identify the events): a drawing program.

  1. Mouse button pressed: start a line.
  2. Mouse moved while pressed: extend the line.
  3. "Clear" button clicked: erase the canvas.
  4. Color button clicked: change the pen color.

Each event triggers its own code segment.

Documentation: a written description of how a program, or part of it, works and how it was developed.

  • Comments are documentation written inside the code. They are ignored when the program runs.
  • Good documentation explains what a segment does and why, not just a restatement of each line.
  • It helps others (and your future self) understand, use, test and maintain the program.
  • It should be written throughout development, not added at the end.
  • Where a programming environment does not allow comments, keep documentation separately.
  • Acknowledge any code not written by you: the author and the source.

In the Create performance task, you make a short video showing your program running. It must show input, the program's functionality, and output. You should also be ready to explain in writing the purpose, how particular procedures and lists work, and how you tested the program.

Common mistakes

  • Giving the function when asked for the purpose.
  • Describing output without saying what input produced it.
  • Treating documentation as an afterthought.
  • Saying comments affect how the program runs.
Check yourself

Which statement describes a program's purpose, as distinct from its function?

  • It stores cities in a list

  • It runs a procedure when a button is clicked

  • It helps users decide what to wear by informing them about the weather

  • It displays the temperature after the user enters a city

Show the answer

It helps users decide what to wear by informing them about the weather

Purpose is the problem being solved; the other choices describe behavior or implementation.

6For top marks

  • Distinguish purpose (why) from function (what it does) clearly; the Create task asks for both.
  • Describe input and output specifically: "the user types a city name; the program displays the five-day forecast".
  • Recognise event-driven behaviour: actions triggered by user events, not a fixed sequence.
  • Explain how documentation supports collaboration and maintenance.
Example

"Purpose: help students revise vocabulary. Function: the program shows a word, accepts the user's typed definition, and displays whether it matches."

Check yourself

A program's function is best described as:

  • Who wrote it

  • What the program does when it runs

  • How fast it runs

  • Why the program was created

Show the answer

What the program does when it runs

Behaviour.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera