LearnViz CompSci
Home / Learn / Programming Fundamentals / Pure Functions

Advanced Topic 48 of 54 Programming Fundamentals

Pure Functions

Functions whose output depends only on inputs and cause no side effects.

How it works

A pure function always returns the same output for the same input and does nothing else: no I/O, no mutation of arguments or globals, no randomness or clock reads. This property, called referential transparency, means a call can be replaced by its result. Pure functions are easy to test, cache, reorder and run in parallel.

Step-by-step walkthrough

  1. Calling f(3) twice with the same input.
  2. always returns the same output.
  3. It does not read or change global state, files or the network.
  4. This makes it easy to test, cache and run in parallel.
  5. Printing or random numbers make a function impure.

Where it is used

Math helpers, formatters, validators, reducers in state-management libraries and business rules are best written as pure functions. Pushing impurity to the outer layer, with a pure core and thin imperative shell, is a widely used architecture.

Common pitfall

Hidden dependencies such as the current time, environment variables, global state or random numbers make a function impure without looking like it. Mutating an input argument is a subtle side effect. Pass such dependencies in as parameters so the function stays testable.

Where it fits in the course

Pure Functions is topic 48 of 54 in Programming Fundamentals, rated Advanced. It follows Functional Programming. The next topic is Immutability in Practice.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Pure Functions?
Functions whose output depends only on inputs and cause no side effects. It belongs to Programming Fundamentals and is an advanced-level topic.
Where do people use Pure Functions?
Math helpers, formatters, validators, reducers in state-management libraries and business rules are best written as pure functions.
What is the most common mistake with Pure Functions?
Hidden dependencies such as the current time, environment variables, global state or random numbers make a function impure without looking like it.
What should I learn before this topic?
Start with Functional Programming, the topic just before it in Programming Fundamentals.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Programming Fundamentals quiz.
Is Pure Functions hard to learn?
It is an Advanced topic. Expect to read the explanation more than once and to connect it with earlier topics. Working through a small example by hand helps most.
How long does it take to learn Pure Functions?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Pure Functions?
Functional Programming, Immutability in Practice, Composition over Inheritance are closely connected. The full list is in the related topics section of this page.
What should I study after Pure Functions?
Continue with Immutability in Practice, the next topic in Programming Fundamentals.

Related topics