Side effects: more than an answer

Concepts explained

A function has a side effect when it does something observable besides returning its answer. Printing a message, drawing a line, changing a shared list, or saving data are side effects.

The name does not mean the effect is bad. Drawing is exactly what you want from a drawing command. It means you should notice that the call changes something.

# language: en
function greet(name):
    say "Hello " + name
    return 7
let result = greet("Mila")
say result

The greeting appears while the function runs. Seven is its separate return value, which the final instruction displays.

Pure functions

A pure function produces its answer from its arguments without changing anything outside the call, and gives the same answer for the same arguments. A small function that doubles a supplied number can be pure. A function that asks the player a question is not pure.

Pure calculations are often easy to test: supply known inputs and compare the answer. For side effects, also check what was displayed, changed, or saved.

A deterministic operation follows a predictable result for the same relevant starting state and inputs. A fixed drawing can be deterministic and still have a side effect. These words answer different questions.

Try calling greet twice. How many greetings appear, even if you do not save both return values?

Return values · Mutation