Function Composition TRB CSI › Python Programming › Function composition

Function Composition

Function composition is the process of combining two or more functions so that the output of one function becomes the input of another function. தமிழில்: ஒரு function return செய்யும் value-ஐ இன்னொரு function argument-ஆக எடுத்துக்கொண்டு pro…

Free Beginner 17 min 145 cards 27 programs 7 MCQs v4
Context

Tamil + English Mixed | Concept First | Inch by Inch | TRB / NET / SET / Interview Style

Function Composition என்பது ஒரு function-ன் output-ஐ மற்றொரு function-க்கு input-ஆக கொடுத்து functions-ஐ ஒன்றாக combine செய்து பயன்படுத்துவது.

  • Function 1
  1. Function 2
  2. Final Output

One function's result becomes another function's input.

Definition
Meaning

Function composition is the process of combining two or more functions so that the output of one function becomes the input of another function.

ஒரு function return செய்யும் value-ஐ இன்னொரு function argument-ஆக எடுத்துக்கொண்டு process செய்வதுதான் Function Composition.

Simple Explanation

நம்மிடம் இரண்டு functions இருக்கிறது

def double(x)

return x * 2

def square(x)

return x * x

double(5)

5 × 2

=

10

double() result

10

இந்த 10-ஐ square() function-க்கு கொடுத்தால்

10 × 10

=

100

இதுதான் function composition.

Python

print(square(double(5)))

Output
100
Concept
Main concept
  1. 5
  2. double()
  3. 10
  4. square()
  5. 100
  • double() output
  • =
  • square() input
  • இதுதான் composition-ன் heart.
Explanation
Why function composition
Core idea

Function composition ஏன் தேவை?

Detail 01

ஒரு பெரிய task-ஐ small-small functions-ஆக பிரித்து எழுதலாம்.

Detail 02
  1. Student Mark
  2. Calculate Percentage
  3. Find Grade
  4. Display Result
Detail 03

ஒவ்வொரு வேலைக்கும் தனித்தனி function.

Detail 04

பிறகு அவற்றை combine செய்யலாம்.

Benefits
  • Code reuse
  • Program easy to understand
  • Testing easy
  • Debugging easy
  • Modular programming
  • Complex problem-ஐ small steps-ஆக divide செய்யலாம்
Detail 06

Suppose:

Syntax
Python 2 lines
1def f(x):
2    return ...
Syntax
Python 2 lines
1def g(x):
2    return ...
Explanation
Composition

g(f(x))

இதன் meaning

First → f(x)

Then → g(result of f(x))

Important
Very important
  1. இந்த expression

    g(f(x))

  2. எந்த function first execute ஆகும்?

  3. Answer

    f(x)

  4. Inner function first execute ஆகும்.

  5. Then

    g(...)

  6. outer function execute ஆகும்.

  7. Memory

    Inside First → Outside Next

Program
Python 2 lines
1def add_five(x):
2    return x + 5
Program
Python 2 lines
1def multiply_two(x):
2    return x * 2
Program
Python 1 lines
1result = multiply_two(add_five(10))
Program
Python 1 lines
1print(result)
Verified output
30
Learning Run Mode — this is the output the author verified, not a live execution.
Output
30
Explanation
First

add_five(10)

Calculation

10 + 5

=

15

So expression becomes

multiply_two(15)

Then

15 × 2

=

30

Final

30

Dry Run
multiply_two(add_five(10))
Execution traceRead top to bottom · highlighted cells show the current value3 states
StepAction
01Iteration 1add_five(10) → ↓ → 10 + 5 → ↓ → 15
02Iteration 2multiply_two(15) → ↓ → 15 × 2 → ↓ → 30
03Final30
Important
  1. Function composition execution order

    Outer(Inner(value))

  2. means

    Inner first

  3. Outer second

Explanation
Example
Core idea

square(double(5))

Execution
  1. double(5)
  2. 10
Detail 02
  1. square(10)
  2. 100
Concept
Mathematical concept

(f ∘ g)(x)

f(g(x))

f composed with g

  • g(x) first
  • f() next
Real Life Example
Example
f(x) = x + 2
g(x) = x × 3
(f ∘ g)(x)
=
f(g(x))

x = 5

g(5)
=
=
15
f(15)
=
=
17

(f ∘ g)(5) = 17

Program
Python program
Python 2 lines
1def f(x):
2    return x + 2
Program
Python 2 lines
1def g(x):
2    return x * 3
Program
Python 1 lines
1print(f(g(5)))
Verified output
17
Learning Run Mode — this is the output the author verified, not a live execution.
Output
17
Explanation
Order matters

Function composition-ல் order மிகவும் முக்கியம்.

Compare

f(g(5))

and

g(f(5))

இரண்டும் same result தர வேண்டிய அவசியமில்லை.

Program
Python 2 lines
1def f(x):
2    return x + 2
Program
Python 2 lines
1def g(x):
2    return x * 3
Program
Python 2 lines
1print(f(g(5)))
2print(g(f(5)))
Verified output
17
21
Learning Run Mode — this is the output the author verified, not a live execution.
Output
17
21
Explanation
Why different
Core idea

f(g(5))

First

g(5)
=
15

Then

f(15)
=
17
g(f(5))

First

f(5)
=
7

Then

g(7)
=
21

So

f(g(x)) ≠ g(f(x))

Detail 06

generally.

TRB Point
Very important exam point
  1. Function composition generally not commutative.

  2. That means

    f(g(x))

  3. and

    g(f(x))

  4. may produce different outputs.

Explanation
Return value connection
Core idea

return value

Previous concept-க்கு direct connection

Function composition வேலை செய்ய முக்கியமானது:

Program
Example
Python 2 lines
1def double(x):
2    return x * 2
Explanation
Core idea

மூலம் output கொடுக்கிறது.

இந்த function

return

Detail 02

அந்த output தான் next function-க்கு input.

Flow
  1. Argument
  2. Function 1
  3. Return Value
  4. Argument to Function 2
  5. Function 2
  6. Final Return Value
Common Mistake
Without return problem

print(x * 2)

return x * x

print(square(double(5)))

இது problem தரும்.

Why?

10

print செய்கிறது.

None

square(None)

None * None

invalid.

Important

Function composition-க்கு:

First function should normally return a value compatible with the next function's parameter.

Input
Input output compatibility
Suppose:
Program
Python 2 lines
1def get_length(text):
2    return len(text)
Program
Python 2 lines
1def double(number):
2    return number * 2

double(get_length("Python"))

Works.

  1. get_length()returns integer
  2. double()expects number
Program
Python 2 lines
1def get_length(text):
2    return len(text)
Program
Python 2 lines
1def double(number):
2    return number * 2
Program
Python 1 lines
1print(double(get_length("Python")))
Verified output
12
Learning Run Mode — this is the output the author verified, not a live execution.
Output
12
Dry Run
Object walkthroughFollow creation, available members and the resulting object state1 stage
  1. Trace

    "Python"

    get_length()


    6

    double()


    12

Real Life Example
Real time example

return mark / 100 * 100

return "A"

return "B"

return "Pass"

return "Fail"

print(grade(percentage(82)))
Output
B
Concept
  1. Mark
  2. Percentage Function
  3. Percentage Value
  4. Grade Function
  5. Grade
Real Life Example
Better real example

return mark / 500 * 100

return "A"

return "B"

return "C"

return "Fail"

print(grade(percentage(400)))
Output
A
Dry Run
Object walkthroughFollow creation, available members and the resulting object state1 stage
  1. Trace
    1. 400
    2. 400 / 500 × 100
    3. 80
    grade(80)

    A
Explanation
String function composition

Function composition numbers மட்டும் இல்லை.

Strings-லையும் செய்யலாம்.

def clean(text)

return text.strip()

def upper(text)

return text.upper()

print(upper(clean(" python ")))

Output
PYTHON
Input
"  python  "

First:

clean()

Output
Result
"python"

Then:

upper()

Output
Result
"PYTHON"
Explanation
Multiple function composition
Core idea

Two functions மட்டும் அல்ல.

Detail 01

பல functions compose செய்யலாம்.

Program
Example
Python 2 lines
1def add_two(x):
2    return x + 2
Program
Python 2 lines
1def double(x):
2    return x * 2
Program
Python 2 lines
1def square(x):
2    return x * x
Program
Python 1 lines
1result = square(double(add_two(3)))
Program
Python 1 lines
1print(result)
Verified output
100
Learning Run Mode — this is the output the author verified, not a live execution.
Output
100
Dry Run
Execution traceRead top to bottom · highlighted cells show the current value5 states
StepAction
01Expressionsquare(double(add_two(3)))
02Firstadd_two(3) → = → 5
03Thendouble(5) → = → 10
04Thensquare(10) → = → 100
05Flow3 → ↓ → add_two() → ↓ → 5 → ↓ → double() → ↓ → 10 → ↓ → square() → ↓ → 100
Explanation
Nested function call
Function composition code often nested function calls போல இருக்கும்

f(g(h(x)))

Execution order
  1. h(x)
  2. g(result)
  3. f(result)
Remember

Deepest function executes first.

Comparison
Step by step vs composition
Core idea

Same code two ways.
Without direct composition
a = add_two(3)
b = double(a)
c = square(b)
print(c)
With composition
print(square(double(add_two(3))))
Both same result.

Explanation
Which is better
Small/simple functions

square(double(add_two(3)))

easy.

But very complex composition

f(g(h(k(m(x)))))

readability குறையலாம்.

அப்போது intermediate variables use செய்வது better.

Program
Example
Python 3 lines
1step1 = add_two(3)
2step2 = double(step1)
3result = square(step2)
Explanation
Function composition with built in functions
Core idea

Built-in functions-யும் compose செய்யலாம்.

Program
Example
Python 1 lines
1print(len(str(12345)))
Program
Execution
Python 9 lines
1str(12345)
2
3"12345"
4
5len("12345")
6
75
8
9print(5)
Verified output
5
Learning Run Mode — this is the output the author verified, not a live execution.
Output
5
Important
  1. Here

    str()

  2. len()

  3. print()

  4. மூன்றும் functions.

  5. Composition

    print(len(str(12345)))

Real Life Example
Another built in example
Context

print(abs(int("-25")))

  1. "-25"
  2. int()
  3. -25
  4. abs()
  5. 25
  6. print()
  7. 25
Explanation
Function composition with lambda
Core idea

Lambda functions-லையும் composition concept பயன்படுத்தலாம்.

Detail 01
double = lambda x: x * 2
square = lambda x: x * x
Detail 02

print(square(double(5)))

Output
100
Explanation
Composition function

நாமே ஒரு generic compose() function எழுதலாம்.

def compose(f, g)

return lambda x: f(g(x))

Then

def double(x)

return x * 2

def square(x)

return x * x

combined = compose(square, double)

print(combined(5))

Output
100
Concept

compose(square, double)

square(double(x))

  1. x
  2. double
  3. square
Explanation
Higher order function connection
Core idea

functions-ஐ arguments-ஆக receive செய்கிறது.

இந்த compose()

def compose(f, g):

Detail 02

So இது Higher-Order Function concept-உடன் தொடர்புடையது.

Higher-order function என்பது

function-ஐ argument-ஆக receive செய்யலாம்
அல்லது function-ஐ return செய்யலாம்

Detail 04

Function composition functional programming-ல் முக்கிய concept.

Comparison
Composition vs nested function
Context

இரண்டையும் confuse செய்யக்கூடாது.
Function Composition
f(g(x))
ஒரு function result மற்ற function input.
Nested Function

pass
ஒரு function definition மற்ற function-க்குள் இருக்கும்.
இரண்டும் different concepts.

Comparison
Function CompositionNested Function
Functions combine outputs/inputsFunction inside function definition
f(g(x))def inner() inside outer
Data flows between functionsFunction structure
Functions separate-ஆக இருக்கலாம்Inner function enclosed
Comparison
Composition vs recursion
Core idea

Composition
Function A calls Function B/result

Explanation
Example
Core idea

f(g(x))
Recursion
Function calls itself

Program
Example
Python 2 lines
1def count(n):
2    count(n - 1)
Verified output
becomes next function:
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
So
Composition
One function with another
Recursion
Function with itself
Concept
Important concept
Context

Function Composition encourages single responsibility.

  • # percentage calculate
  • # grade calculate
  • # message format
  • # display
  1. calculate_percentage()
  2. find_grade()
  3. format_result()Each function one job.Then compose.
Common Mistake
Error 1
Context

Wrong execution order

f(g(x))

Don't think f() first.

  • g() first
  • f() next
Common Mistake
Error 2
Context

First function doesn't return value

print(x + 1)

return x * 2

b(a(5))

a() returns None.

Composition fails if b() expects a number.

Common Mistake
Error 3
Context

Incompatible return/input types

return "Python"

return x * x

square(get_name())

"Python" * "Python"

invalid.

So function types must be compatible.

Common Mistake
Error 4

Assuming order doesn't matter
f(g(x))

and:

g(f(x))

are generally different.

Common Mistake
Error 5

Too much nesting
a(b(c(d(e(f(x))))))

technically possible.

But readability becomes difficult.

Use intermediate variables when needed.

Memory Trick

Output → Input

Output
becomes next function:
Input
Easy Tamil:

ஒரு function கொடுப்பதை அடுத்த function வாங்கும்.

Memory Trick
Another memory trick

For:

f(g(x))

Remember
  1. Goes Inside First

  2. g() is inside.

  3. So

    g first

  4. f next

TRB Point

1. Function composition means?

TRB Point

One function's output is passed to another function as input.

TRB Point

1. In f(g(x)), which executes first?

TRB Point

g(x)

TRB Point

1. Is f(g(x)) always equal to g(f(x))?

TRB Point

No

TRB Point

1. Function composition mainly depends on?

TRB Point

Return values

TRB Point

1. First function's return value should be?

TRB Point

Compatible with the next function's expected input.

TRB Point

1. Can built-in functions be composed?

TRB Point

Yes

TRB Point

1. Can lambda functions be composed?

TRB Point

Yes

TRB Point

1. Is function composition same as recursion?

TRB Point

No

TRB Point

1. Is function composition same as nested function definition?

TRB Point

No

TRB Point

1. Mathematical notation for composition?

TRB Point

(f ∘ g)(x) = f(g(x))

Interview Point

What is function composition?

Function composition combines functions by passing the return value of one function as the argument to another function.

Program
Example
Python 2 lines
1result = square(double(5))
2Which function executes first in f(g(x))?
Program
Python 1 lines
1The inner function g(x) executes first.
Verified output
25
30
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

Its result is passed to f().

Reason

is return important in function composition?

Reason

the output from one function must be available to pass as input to another function.

Detail 03

Is function composition order-sensitive?

Detail 04

Yes.

In general

f(g(x)) ≠ g(f(x))
What is the advantage of function composition?

Detail 06

It helps create reusable, modular, testable, and easier-to-maintain code.

MCQ
127Function composition means:
Choose one answer
MCQ
128In: f(g(5)) which executes first?
Choose one answer
MCQ
129Mathematical composition: (f ∘ g)(x) means:
Choose one answer
Dry Run
Object walkthroughFollow creation, available members and the resulting object state1 stage
  1. Trace

    a(4) = 6
    b(6) = 18
    Answer: 18

MCQ
131Which concept is most important for composition?
Choose one answer
MCQ
132Is f(g(x)) always same as g(f(x))?
Choose one answer
MCQ
133What does the inner function's return value become?
Choose one answer
MCQ
134Can three functions be composed?
Choose one answer
Explanation
Example
Core idea

f(g(h(x)))

List
  1. Function composition promotes:
Explanation
Core idea
  • A. Code repetition
  • B. Modularity
  • C. Syntax errors
  • D. Infinite loops
Answer

B

List
  1. Function composition and recursion are:
Explanation
Core idea

A. Always same
B. Different concepts

Answer

B

Practice
Practice 1
def add10(x):
    return x + 10

def double(x):
    return x * 2

print(double(add10(5)))

Find output.

Step:

add10(5) = 15
double(15) = 30

Answer:

30

Practice
Practice 2
def square(x):
    return x * x

def minus_one(x):
    return x - 1

print(minus_one(square(4)))

Step:

square(4) = 16
minus_one(16) = 15

Answer:

15

Practice
Practice 3

Find difference:

def f(x):
    return x + 5

def g(x):
    return x * 2

print(f(g(10)))
print(g(f(10)))

First:

g(10) = 20
f(20) = 25

Second:

f(10) = 15
g(15) = 30

Output
25
30
Summary
  1. Function Composition
  2. Input
  3. Function A
  4. Return Value
  5. Function B
  6. Final Return Value
  7. Core syntax
  8. f(g(x))
  9. Execution
  10. g(x) first
  11. result
  12. f(result)
  13. Most important ideas
  14. Output of one function
  15. =
  16. Input of next function
  17. f(g(x)) generally ≠ g(f(x))
  18. Composition depends heavily onreturn values
  19. And remember
  20. Function Composition = Smallfunctions சேர்ந்து ஒரு பெரிய வேலை செய்வது.
Explanation
Short description
Core idea

Function Composition என்பது ஒரு function return செய்யும் output-ஐ மற்றொரு function-க்கு argument/input-ஆக கொடுத்து functions-ஐ combine செய்வது. f(g(x)) என்ற composition-ல் முதலில் g(x) execute ஆகும்; அதன் return value f()-க்கு input ஆகும். இது modular, reusable மற்றும் clean programming-க்கு மிகவும் முக்கியமான concept.

Text size
17px
Theme
Contents
Print / PDF
Program