Definition of a Function: Domain, Codomain, Range and Counting Formula
Build the definition from first principles, separate the three sets, verify one finite map, and count all functions before handling common restrictions.
KnowledgeGate Team
Exam prep & CS education

A function is not just an equation. Its codomain need not equal its range. Its count also depends on which set supplies the inputs. A function assigns each input exactly one output, and finite function counts follow from the available choices and any restrictions. For broader preparation context, use the GATE category.
Related reading: Surjective functions and Inverse functions.
What makes a relation a function?
Write a mapping as f: A -> B. It is a function if every element of the domain A is assigned to exactly one element of the codomain B.
That definition contains two separate checks:
No element of
Acan be left without an image.No element of
Acan have two different images.
Let A = {1, 2, 3} and B = {p, q, r}. The relation {(1, p), (2, p), (3, q)} is a function. Inputs 1 and 2 may share p, and the unused element r causes no problem.
In contrast, {(1, p), (1, q), (2, r), (3, p)} fails because input 1 has two images. The relation {(1, p), (2, q)} fails from A because input 3 has none.
This ordered-pair language comes from relations. Review Set Theory and Relations Explained for GATE if needed. Repeated outputs are permitted, but multiple outputs from one input are not.
Domain, codomain and range are different sets
The domain is the declared input set. The codomain is the declared target set. The range is the set of outputs actually attained:
range(f) = {f(x) | x in A}
Therefore, range(f) subseteq B.
For h: {0, 1, 2, 3} -> {1, 2, 3, 4}, with h(x) = x + 1, the domain is {0, 1, 2, 3} and both codomain and range are {1, 2, 3, 4}. Change only the codomain to {1, 2, 3, 4, 5}. The range stays {1, 2, 3, 4}, leaving 5 in the codomain but outside the range.
A formula must be read with its sets. For g: R \ {2} -> R, where g(x) = 1/(x - 2), the output cannot be 0; every nonzero real y is reached by x = 2 + 1/y. Thus the range is R \ {0}. Declaring g: R -> R would fail because input 2 has no real output.
Worked example: verify a finite map and find all three sets
Take:
A = {1, 2, 3, 4}B = {a, b, c}f = {(1, a), (2, c), (3, a), (4, c)}
The first coordinates 1, 2, 3, and 4 each occur exactly once, so every domain element has one image. The second coordinates a and c both belong to B. Therefore, f: A -> B is valid.
Read the three sets separately:
Domain:
{1, 2, 3, 4}Codomain:
{a, b, c}Range:
{a, c}
The unused b remains in the codomain. The map is many-to-one because f(1) = f(3) = a and f(2) = f(4) = c. It is not onto B because b is absent from the range.

How many functions from A to B are possible?
Suppose |A| = m and |B| = n. Each labelled input independently chooses one of n images, repeated across all m inputs:
n x n x ... x n = n^m
The codomain size is the base, the choices per input. The domain size is the exponent, the number of input slots.
For the worked sets, four inputs each have three possible images:
3 x 3 x 3 x 3 = 3^4 = 81
A function is represented by (f(1), f(2), f(3), f(4)); the worked map is (a, c, a, c).
From the empty domain to any codomain there is one empty function. From a non-empty domain to the empty codomain there is no function. These boundary cases are clearer when stated directly; no use of the ambiguous notation 0^0 is needed.

Count functions when restrictions are added
Reuse A = {1, 2, 3, 4} and B = {a, b, c}.
If f(1) = a and f(4) = c are fixed, two inputs remain free. Each has three choices, so there are 3 x 3 = 3^2 = 9 functions. Fixing k compatible pairs generally leaves n^(m-k) functions.
If the range must be contained in {a, c}, each of four inputs has two choices. The count is 2^4 = 16. If the range must be exactly {a, c}, both values must occur. Remove the all-a and all-c functions, giving 16 - 2 = 14.
For "use a at least once", count the complement. Of 81 functions, 2^4 = 16 avoid a by using only b and c. Therefore, 81 - 16 = 65 use a.
Exactly three functions have a one-element range, one constant function for each of a, b, and c.
Common function traps and their corrections
Treating codomain and range as synonyms: Here the codomain is
{a, b, c}, but the range is{a, c}. Write the target first, then collect attained outputs.Rejecting repeated outputs:
{(1, a), (2, a)}can be part of a function. In contrast,{(1, a), (1, b)}cannot, because one input has two images.Reversing the exponent: Four input slots with three choices each give
3^4 = 81, not4^3 = 64. Say, "choices per input raised to the number of inputs."Ignoring the declared mapping: In
g(x) = 1/(x - 2), the rule excludes2. A declaration that requires2as an input does not define a total function.
How questions test function definitions and counts
Questions may ask whether ordered pairs form a function, request the three sets from an arrow map, repair a missing or double image, count all finite functions, or add image and range restrictions.
Try a 60-second drill. Let X = {u, v, w} and Y = {0, 1}.
Three inputs each have two choices, so there are
2^3 = 8functions fromXtoY.If
f(u) = 0, two inputs remain free, so there are2^2 = 4functions.To be onto, the function must use both
0and1. Of the eight functions, two are constant, so exactly8 - 2 = 6are onto.
The general count n^m allows repeated images and unused codomain values. Bijective Function: Definition, Count Formula and Key Properties handles the stricter case in which every codomain value is used exactly once, reducing the count to n! for equal-sized sets.
The short version and next step
Every domain element needs exactly one image. The codomain is declared, while the range is attained. Write range(f) subseteq codomain. With m inputs and n choices each, the count is n^m.
Redraw the four-input map and recover its domain, codomain, and range without looking. Then replace f(4) = c with f(4) = b. The result is still a function, its range becomes {a, b, c}, and the number of possible functions from A to B remains 81. Add a second arrow 1 -> b, and the relation stops being a function because input 1 now has two images.
For broader structured preparation, GATE Guidance by Sanchit Sir gives a route across GATE subjects. Engineering Mathematics for GATE Exam supports the counting, permutation, and probability foundation used around this topic.
Keep learning

Classification of Finite and Infinite Groups: Orders, Cyclicity and Worked Examples
Learn how group axioms, group order, element order and generators classify finite and infinite groups through complete, checkable examples.

Bipartite, Cycle, Regular and Complement Graphs: Tests, Formulas and a C6 Worked Example
Learn a dependable order for classifying simple graphs, then apply it to C6 and list, count and test every edge in its complement.

Basic Terminologies in Linear Programming: Feasible Solutions, BFS and an Optimal Solution Worked Step by Step
Separate feasible, basic feasible and optimal solutions through geometry, slack variables, vertex enumeration and concise counterexamples.

Assignment Problem and Hungarian Method: Formulation with a Complete Worked Example
Learn why greedy assignment fails, how matrix reductions preserve the optimum, and how the uncovered-value adjustment leads to a minimum cost of 140.