Onto Function — Definition, Formula, Graph, and Examples

#Algebra
TL;DR
An onto function (also called a surjective function) is one where every element of the codomain is the output of at least one input — nothing in the target set is left unreached. This article gives the formal definition, the test (range equals codomain), the formula for the number of onto functions, the onto-vs-into contrast, and six worked examples
BT
Bhanzu TeamLast updated on July 14, 20269 min read

What Is An Onto Function?

An onto function $f: A \to B$ is one where every element of the codomain $B$ is the image of at least one element of the domain $A$. Written formally: for every $y \in B$, there exists at least one $x \in A$ such that $f(x) = y$. The textbook synonym is a surjective function — the map sends $A$ onto all of $B$. The companion article on the surjective function walks the same idea through a formal onto-proof if you want the proof-heavy version.

There is no uniqueness condition. Two different inputs may land on the same output; the only thing forbidden is leaving an output unreached. The one-line working test is this:

A function is onto exactly when its range equals its codomain.

  • Codomain — the target set you declare when you write $f: A \to B$. It is a choice.

  • Range — the set of outputs the function actually produces.

The range always sits inside the codomain. Onto means the two are the same set, with nothing left over.

How Do You Check If A Function Is Onto?

A common search is simply how do you know if a function is onto? The procedure runs backwards, starting from an arbitrary output:

  1. Take an arbitrary element $y$ in the codomain $B$.

  2. Solve $f(x) = y$ for $x$.

  3. Confirm that solution $x$ actually lies in the domain $A$.

If you can always produce such an $x$, every output is reachable and the function is onto. On a graph, the parallel idea is the horizontal line test for onto: a function is onto its codomain when every horizontal line drawn through the codomain meets the graph at least once.

What Is The Difference Between An Onto Function And An Into Function?

This is the disambiguation question that drives most of the search traffic, so it earns its own section. The two terms are complements:

  • Onto (surjective): every element of $B$ is reached. Range $=$ codomain.

  • Into: at least one element of $B$ is not reached by any input. Range is a proper subset of the codomain.

Every function is either onto or into — there is no third option once the codomain is fixed. The classic example is $f(x) = x^2$ written as $f: \mathbb{R} \to \mathbb{R}$. No negative number is ever an output, so $-4$ has no preimage. The range is $[0, \infty)$, a proper subset of $\mathbb{R}$, which makes the function into. The into function article works this contrast from the other side. Rewrite the codomain as $f: \mathbb{R} \to [0, \infty)$ and the same rule becomes onto.

What Is The Formula For The Number Of Onto Functions?

When $A$ has $m$ elements and $B$ has $n$ elements, the number of onto functions from $A$ to $B$ is counted by inclusion-exclusion:

$$\text{Number of onto functions} = \sum_{k=0}^{n} (-1)^k \binom{n}{k} (n-k)^m$$

A few consequences worth carrying:

  • If $m < n$, there are 0 onto functions — there aren't enough inputs to cover every output.

  • If $m = n$, the count is $n!$, and every onto function in this case is also one-one (a bijective function).

  • The formula relies on the binomial theorem coefficient $\binom{n}{k}$, which counts how many ways $k$ outputs get left out.

Each symbol means: $m$ is the size of the domain, $n$ the size of the codomain, $\binom{n}{k}$ the number of ways to choose $k$ codomain elements to exclude, and $(n-k)^m$ the number of functions mapping into the remaining $n-k$ outputs.

Examples Of Onto Functions

The six examples below move from a tiny finite map to a counting problem, with a deliberate wrong turn in the middle.

Example 1

Decide whether $f: {1, 2, 3} \to {a, b}$ given by $f(1)=a,\ f(2)=b,\ f(3)=b$ is onto.

Check each codomain element for an incoming arrow.

$a$ is reached by $1$.

$b$ is reached by $2$ and $3$.

Both elements of ${a, b}$ are reached, so the function is onto. The repeated output ($b$ twice) is allowed.

Example 2

Decide whether $f: \mathbb{R} \to \mathbb{R}$, $f(x) = x^2$, is onto.

The tempting first move is to say "every output has a square root, so it's onto."

Test it on a specific output. Pick $y = -4$ in the codomain $\mathbb{R}$.

Solve $x^2 = -4$.

There is no real $x$ whose square is $-4$.

So $-4$ is never produced. The "every output has a square root" instinct quietly assumed the output was non-negative, but the declared codomain is all of $\mathbb{R}$, negatives included.

The function is not onto; its range is only $[0, \infty)$. Fix the codomain to $[0, \infty)$ and it becomes onto. The first instinct to skip the negative outputs is exactly where this one goes wrong.

Example 3

Show that $f: \mathbb{R} \to \mathbb{R}$, $f(x) = 2x - 1$, is onto.

Take an arbitrary $y \in \mathbb{R}$.

Solve $2x - 1 = y$.

$2x = y + 1$.

$x = \dfrac{y + 1}{2}$.

This $x$ is a real number for every real $y$, so it lies in the domain.

Every output is reachable, so $f$ is onto.

Example 4

Decide whether $f: \mathbb{R} \to \mathbb{R}$, $f(x) = x^3$, is onto.

Take an arbitrary $y \in \mathbb{R}$.

Solve $x^3 = y$.

$x = \sqrt[3]{y}$.

Every real number has a real cube root, so $x$ is always in the domain.

The function is onto (and, since cubing is also one-one, it is bijective).

Example 5

Count the onto functions from a 3-element set to a 2-element set.

Here $m = 3$, $n = 2$.

$$\sum_{k=0}^{2} (-1)^k \binom{2}{k}(2-k)^3 = \binom{2}{0}2^3 - \binom{2}{1}1^3 + \binom{2}{2}0^3$$

$$= 8 - 2 + 0 = 6$$

There are 6 onto functions. (Total functions would be $2^3 = 8$; the two we subtract are the constant maps that hit only one output.)

Example 6

A factory has 4 machines, each routed to exactly one of 3 output bins. How many routings use every bin at least once?

This is "onto functions from a 4-element set to a 3-element set," so $m = 4$, $n = 3$.

$$\sum_{k=0}^{3}(-1)^k \binom{3}{k}(3-k)^4 = 3^4 - \binom{3}{1}2^4 + \binom{3}{2}1^4 - \binom{3}{3}0^4$$

$$= 81 - 48 + 3 - 0 = 36$$

So 36 routings leave no bin empty. The real-world framing — "every bin used" — is just surjectivity in disguise.

Why The Codomain Matters: "Onto Is A Claim About Coverage"

Onto exists because mathematics needed a precise way to say the map covers the whole target. That single idea decides whether a function can be reversed at all.

  • It is the gatekeeper for inverses. A function can be undone only if it is onto (and one-one). If outputs are missing, there is nothing to map them back from. This is the foundation under every inverse function.

  • It powers counting arguments. Cryptographic key assignment, hashing into a fixed number of buckets, and scheduling problems all ask "does every slot get used?" — a surjectivity question.

  • It underwrites the pigeonhole reasoning behind why a finite map can be onto only when the domain is at least as large as the codomain.

The destination this opens up is cardinality: comparing the sizes of infinite sets. The mathematician Georg Cantor used onto and one-one maps in the 1870s to prove that some infinities are genuinely larger than others — a result so unsettling at the time that it drew open hostility before it became a cornerstone of modern mathematics.

Tripping Points To Avoid

Three mistakes account for most wrong answers on onto-function questions.

Mistake 1: Confusing range with codomain

Where it slips in: any question where the function's formula produces fewer outputs than the declared target set.

Don't do this: declare $f: \mathbb{R} \to \mathbb{R}$, $f(x) = e^x$ onto because "exponentials grow without bound."

The correct way: check the full codomain. $e^x$ is always positive, so $0$ and every negative number are never outputs. Range is $(0, \infty)$, not $\mathbb{R}$, so it is into, not onto. The memorizer who learned "$e^x$ is a standard function" without checking the codomain is the one who reaches for the wrong answer here.

Mistake 2: Treating a repeated output as a failure

Where it slips in: finite mapping diagrams where two inputs point at the same output.

Don't do this: call $f(1)=a,\ f(2)=a,\ f(3)=b$ (codomain ${a,b}$) not-onto because "$1$ and $2$ collide."

The correct way: collisions are irrelevant to onto-ness. Ask only whether every output is reached. Both $a$ and $b$ are reached, so it is onto. (Collisions matter for one-one, a different property — see the injective function.) The rusher who pattern-matches "two arrows on one dot = broken" trips here.

Mistake 3: Forgetting that onto depends on the codomain

Where it slips in: problems that hand you a formula without naming the codomain.

Don't do this: answer "onto" or "not onto" before the target set is fixed.

The correct way: treat the codomain as part of the question. $f(x) = x^2$ is not onto as $\mathbb{R}\to\mathbb{R}$ but is onto as $\mathbb{R}\to[0,\infty)$. Same formula, opposite answer.

Conclusion

  • An onto function reaches every element of its codomain — range equals codomain.

  • Onto and into are complements: into leaves at least one output unreached.

  • To check ontoness, solve $f(x) = y$ for an arbitrary $y$ and confirm $x$ stays in the domain.

  • The number of onto functions from an $m$-set to an $n$-set is $\sum_{k=0}^{n}(-1)^k\binom{n}{k}(n-k)^m$.

  • Ontoness depends on the declared codomain, and it is half of what a function needs to be invertible.

To take the idea further with a teacher who explains the why before the procedure, explore Bhanzu's algebra tutor, get focused help with algebra, or browse math classes online. Want a live Bhanzu trainer to walk through onto-function proofs with your child? Book a free demo class.

Read More

Book a Free Demo

Was this article helpful?

Your feedback helps us write better content

Frequently Asked Questions

Is an onto function the same as a surjective function?
Yes. "Onto" and "surjective" are exact synonyms — onto is the everyday term, surjective the formal one
Can a function be both onto and one-one?
Yes. A function that is both onto and one-one is called bijective, and it is precisely the kind of function that has an inverse.
How do you prove a function is not onto?
Find a single element of the codomain with no preimage. One missed output is enough to break ontoness.
Is every linear function $f(x)=ax+b$ onto $\mathbb{R}$?
Only when $a \neq 0$. A non-constant line covers all of $\mathbb{R}$; a constant function ($a=0$) hits just one value and is into.
Why does the number of onto functions need inclusion-exclusion?
Because you start from all functions, then subtract the ones that miss one output, add back those over-subtracted for missing two, and so on — that alternating add-subtract pattern is inclusion-exclusion.
✍️ Written By
BT
Bhanzu Team
Content Creator and Editor
Bhanzu’s editorial team, known as Team Bhanzu, is made up of experienced educators, curriculum experts, content strategists, and fact-checkers dedicated to making math simple and engaging for learners worldwide. Every article and resource is carefully researched, thoughtfully structured, and rigorously reviewed to ensure accuracy, clarity, and real-world relevance. We understand that building strong math foundations can raise questions for students and parents alike. That’s why Team Bhanzu focuses on delivering practical insights, concept-driven explanations, and trustworthy guidance-empowering learners to develop confidence, speed, and a lifelong love for mathematics.
Related Articles
Book a FREE Demo ClassBook Now →