Skip to topic
    ← Back to course topics

    Composition of functions — AQA A-Level Computer Science

    Function composition is the process of combining two or more functions such that the output of one function becomes the direct input of the next. In AQA A-Level Computer Science functional programming, the composite function (f . g)(x) denotes f(g(x)), where g is evaluated first and its codomain must match the domain of f.

    Composition of functions explained

    Functional composition is an operation that takes two functions, f and g, and produces a third function that applies them in sequence.

    Read the full explanation

    The composition of f after g, written f ∘ g, is defined by (f ∘ g)(x) = f(g(x)): the input x is first passed to g, and the result of that is then passed to f. The output of g therefore becomes the input of f, so the two functions must be compatible, meaning the output type of g must fit the input type of f. Because the result is itself a function, composition can be chained and reused. For example, if g(x) = x + 1 and f(x) = 2x, then (f ∘ g)(3) = f(g(3)) = f(4) = 8, whereas (g ∘ f)(3) = g(f(3)) = g(6) = 7. This shows that composition is generally not commutative, so the order of the functions matters.

    Your focus

    1. The operation functional composition combines two functions to get a new function.

    Which command word is this?

    Provide a concise answer without extensive explanation, such as writing a final type signature or numeric result.

    Composition of functions exam tips

    Topic Overview

    Composition of functions is a core paradigm of functional programming within the AQA A-Level Computer Science specification. It involves combining two simpler functions to create a new, more complex function without manually writing intermediate variable assignments.

    This technique allows software engineers to build clean, modular, and declarative programs by chaining operations together. Understanding functional composition strengthens computational thinking and links mathematical logic directly to practical programming constructs.

    Key Concepts
    • →Definition of composition: (f . g)(x) is mathematically defined as f(g(x)), executing g first and then f.
    • →Type matching rule: Function composition (f . g) is only valid if the codomain of g is identical to or compatible with the domain of f.
    • →Associativity: Function composition is associative, meaning (f . (g . h)) is equivalent to ((f . g) . h).
    • →First-class functions: Composition relies on the fact that functions can be treated as first-class objects that can be accepted as arguments and returned from other functions.
    Marking Points
    • Identifies that the result of composition is a new function rather than a single value.
    • States that the functions are applied in sequence, with one function's output used as the other's input.
    • Recognises that f ∘ g applies g first and f second, which is the reverse of the order in which the notation is read.
    • Notes that composition is generally not commutative, so f ∘ g and g ∘ f can give different results.
    • Explains that the output type of the first function must match the input type of the second.
    Examiner Tips
    • 💡When evaluating a composed function at a point, work from the inside outwards, computing the innermost function first.
    • 💡Check the order of the functions in the notation before calculating, since it determines the result.
    • 💡Use a simple numerical example to test whether two functions commute if asked to justify a claim about order.
    • 💡When asked to trace or evaluate composed functions, always write out the intermediate step f(g(x)) to demonstrate your working.
    • 💡Always reference both 'domain' and 'codomain' when answering questions about whether a composition is valid.
    • 💡Remember that the composition operator '.' produces a brand-new function; it does not evaluate until it receives its final argument.
    Common Mistakes
    • Treats composition as adding or multiplying the two functions; correct it by recognising that the functions are applied one after another to an input.
    • Reverses the order of application in f ∘ g; correct it by applying the right-hand function first.
    • Assumes f ∘ g and g ∘ f always produce the same result; correct it with a worked numerical example showing they can differ.
    • Thinking that (f . g)(x) means f is evaluated before g; in functional programming, evaluation moves from right to left (inside to outside).
    • Assuming any two functions can be composed regardless of their input and output types, ignoring domain/codomain type constraints.
    • Confusing function composition with applying multiple arguments to a single curried function.
    Revision Plan
    1. 1Day 1-2: Review functional programming basics, domains, codomains, and pure functions.
    2. 2Day 3-4: Practise converting expressions between f(g(x)) and (f . g)(x) with numeric functions.
    3. 3Day 5-6: Solve problems involving type matching and non-numeric types (e.g., String, Boolean, Lists).
    4. 4Day 7: Complete past exam questions on tracing and defining composite functions under timed conditions.
    Exam Question Types
    • 📋Evaluation questions: Given function definitions and an input, trace and calculate the final return value of a composite function.
    • 📋Type signature deduction: Given two functions with defined types, write out the type signature of their composition.
    • 📋Validity explanation: Determine whether two given functions can be composed and explain why using domain and codomain terminology.
    Command Word Expectations (AQA)
    State

    Provide a concise answer without extensive explanation, such as writing a final type signature or numeric result.

    Explain

    Give reasons or causes for a statement, such as justifying why a composition is valid with reference to domain and codomain rules.

    Calculate

    Perform mathematical or logical evaluation step by step to arrive at a definitive final value.

    How Students Lose Marks (Examiner Pitfalls)
    Pitfall: Confusing the execution order of composed functions written with mathematical infix notation.
    ❌ Weak Answer (Loses Marks):In the expression (f . g)(x), f is applied to x first, and then g is applied to the result.
    Example improved answer:In the composite function (f . g)(x), the function g is applied first to the argument x, yielding g(x). The function f is then applied to the output of g, evaluating as f(g(x)). The composition operator (.) evaluates right-to-left.
    Examiner Tip: Always write out intermediate types and bracket the inner function as f(g(x)) to avoid evaluating the operations in reverse order.
    Pitfall: Overlooking type compatibility between the domain and codomain of composed functions.
    ❌ Weak Answer (Loses Marks):Any two functions can be composed together as long as they take numbers as inputs.
    Example improved answer:Two functions f and g can only be composed to form (f . g) if the codomain of g is a subset of, or matches, the domain of f. If the types do not align, a type mismatch error occurs.
    Examiner Tip: State the type signatures explicitly when justifying whether two functions can be composed.
    Step-by-Step Worked Solutions

    Question: Two functions are defined as follows: inc :: Int -> Int inc x = x + 1 dbl :: Int -> Int dbl x = x * 2 Calculate the result of (inc . dbl) 4 and explain why the composition is valid with reference to types.

    1. 1.Step 1: Identify the order of execution. The composite function (inc . dbl) 4 is equivalent to inc(dbl(4)).
    2. 2.Step 2: Evaluate the inner function first: dbl(4) = 4 * 2 = 8.
    3. 3.Step 3: Evaluate the outer function using the result of Step 2: inc(8) = 8 + 1 = 9.
    4. 4.Step 4: Check type compatibility. dbl has the signature Int -> Int, meaning its codomain is Int. inc has the signature Int -> Int, meaning its domain is Int. Since the codomain of dbl matches the domain of inc, the composition is valid.
    Final Answer: (inc . dbl) 4 = 9. The composition is valid because the codomain of dbl (Int) matches the domain of inc (Int).

    Question: Consider the functions: isEven :: Int -> Bool isEven n = (n `mod` 2 == 0) strLen :: String -> Int strLen s = length s Write the type signature of the composed function (isEven . strLen) and evaluate (isEven . strLen) 'Mastery'.

    1. 1.Step 1: Determine the domain and codomain of each function. strLen takes a String and outputs an Int. isEven takes an Int and outputs a Bool.
    2. 2.Step 2: Check type compatibility. The output of strLen (Int) matches the input of isEven (Int).
    3. 3.Step 3: Formulate the type signature of the composite function. Input is String, final output is Bool, so (isEven . strLen) :: String -> Bool.
    4. 4.Step 4: Evaluate the expression on the argument 'Mastery'. First evaluate strLen('Mastery') = 7.
    5. 5.Step 5: Apply isEven to the intermediate result: isEven(7) = False.
    Final Answer: Type signature: (isEven . strLen) :: String -> Bool. Result: False.
    Active Recall Memory Test
    What does the expression (f . g)(x) evaluate to in expanded form?
    Key Fact: f(g(x))
    What condition must be satisfied regarding types for two functions f and g to be composed as (f . g)?
    Key Fact: The codomain of g must match (or be a subtype of) the domain of f.
    Which function executes first in the composite expression (h . k)(y)?
    Key Fact: k executes first, receiving y as its input.
    Is function composition commutative? Explain briefly.
    Key Fact: No. In general, (f . g) does not equal (g . f).
    Frequently Asked Questions
    What is the difference between function composition and calling a function with multiple parameters?
    Function composition combines two separate single-parameter functions into a single new function by passing the output of one directly into the input of the other. In contrast, calling a function with multiple parameters provides multiple distinct inputs to a single function. In functional programming, composition enables point-free programming where data arguments do not need to be explicitly declared.
    Why does function composition evaluate from right to left?
    Function composition evaluates from right to left because it mirrors standard mathematical notation. When written as (f . g)(x), it expands to f(g(x)). In this nested format, parentheses dictate that the inner expression g(x) must be evaluated before the outer function f can receive its argument.
    Can I compose more than two functions at once?
    Yes, you can chain any number of functions together, such as (f . g . h)(x), provided each intermediate function's codomain matches the next function's domain. Because function composition is associative, (f . (g . h)) is identical to ((f . g) . h). The functions will execute from right to left: h first, then g, and finally f.
    What does point-free style mean in the context of function composition?
    Point-free style (or tacit programming) is a way of defining functions without explicitly naming or referencing the arguments (points) on which they operate. For example, instead of writing 'process x = f(g(x))', you can simply define 'process = f . g'. This leads to cleaner, more concise code in functional languages like Haskell.