Learning path

Full curriculum

Full curriculum

Unit content

Input and output

A program becomes useful by exchanging data with its environment. Input brings data into the program; output sends data or effects back out.

Standard input and output

A command-line program may read text from standard input and write text to standard output.

For example, a program might read

12 7

parse the two textual values as integers, calculate their sum and output

19

Parsing and formatting

External data often arrives in a representation different from the internal value a program needs.

Parsing interprets input text or bytes as structured values. Formatting converts internal values into a representation suitable for output.

Invalid input must be handled explicitly because not every sequence of characters can be interpreted as the expected type.

Other forms of I/O

Files, network connections, keyboards, sensors, displays and web requests are all forms of input and output. They differ in mechanism but share the same boundary between the program and something outside it.

Side effects

Input and output are examples of side effects: the program interacts with state beyond a pure calculation of values.

Separating core computation from I/O often makes programs easier to test and reason about.