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…
FreeBeginner 17 min 145 cards 27 programs 7 MCQsv4
Context
Tamil + English Mixed | Concept First | Inch by Inch | TRB / NET / SET / Interview Style
Function Composition என்பது ஒரு function-ன் output-ஐ மற்றொரு function-க்கு input-ஆக கொடுத்து functions-ஐ ஒன்றாக combine செய்து பயன்படுத்துவது.
01Simple idea
Function 1
↓
02Output
1Function 2
2Final Output
03அதாவது
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.
01தமிழில்
ஒரு 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
01Function composition-ஐ இப்படிப் புரிந்து கொள்ளுங்கள்
15
2double()
310
4square()
5100
02இங்கே
double() output
=
square() input
இதுதான் composition-ன் heart.
Explanation
Why function composition
Core idea
Function composition ஏன் தேவை?
01Detail 01
ஒரு பெரிய task-ஐ small-small functions-ஆக பிரித்து எழுதலாம்.
02Detail 02
1Student Mark
2Calculate Percentage
3Find Grade
4Display Result
03Detail 03
ஒவ்வொரு வேலைக்கும் தனித்தனி function.
04Detail 04
பிறகு அவற்றை combine செய்யலாம்.
05Benefits
Code reuse
Program easy to understand
Testing easy
Debugging easy
Modular programming
Complex problem-ஐ small steps-ஆக divide செய்யலாம்
06Detail 06
Suppose:
Syntax
Python2 lines
1deff(x):2return ...
Syntax
Python2 lines
1defg(x):2return ...
Explanation
Composition
g(f(x))
இதன் meaning
First → f(x)
Then → g(result of f(x))
Important
Very important
01இந்த expression
g(f(x))
02
எந்த function first execute ஆகும்?
03Answer
f(x)
04
Inner function first execute ஆகும்.
05Then
g(...)
06
outer function execute ஆகும்.
07Memory
Inside First → Outside Next
Program
Python2 lines
1defadd_five(x):2return x + 5
Program
Python2 lines
1defmultiply_two(x):2return x * 2
Program
Python1 lines
1result = multiply_two(add_five(10))
Program
Python1 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
Step
Action
01Iteration 1
add_five(10) → ↓ → 10 + 5 → ↓ → 15
02Iteration 2
multiply_two(15) → ↓ → 15 × 2 → ↓ → 30
03Final
30
Important
01Function composition execution order
Outer(Inner(value))
02means
Inner first
03
Outer second
Explanation
Example
Core idea
square(double(5))
01Execution
1double(5)
210
02Detail 02
1square(10)
2100
Concept
Mathematical concept
01Mathematics-ல் function composition
(f ∘ g)(x)
02means
f(g(x))
03Read
f composed with g
04முக்கியமாக
g(x) first
f() next
Real Life Example
Example
01Suppose
f(x) = x + 2
g(x) = x × 3
02Then
(f ∘ g)(x)
=
f(g(x))
03For
x = 5
04First
g(5)
=
055 × 3
=
15
06Then
f(15)
=
0715 + 2
=
17
08So
(f ∘ g)(5) = 17
Program
Python program
Python2 lines
1deff(x):2return x + 2
Program
Python2 lines
1defg(x):2return x * 3
Program
Python1 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
Python2 lines
1deff(x):2return x + 2
Program
Python2 lines
1defg(x):2return x * 3
Program
Python2 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))
01First
g(5)
=
15
02Then
f(15)
=
17
g(f(5))
03First
f(5)
=
7
04Then
g(7)
=
21
05So
f(g(x)) ≠ g(f(x))
06Detail 06
generally.
TRB Point
Very important exam point
01
Function composition generally not commutative.
02That means
f(g(x))
03and
g(f(x))
04
may produce different outputs.
Explanation
Return value connection
Core idea
return value
01Previous concept-க்கு direct connection
Function composition வேலை செய்ய முக்கியமானது:
Program
Example
Python2 lines
1defdouble(x):2return x * 2
Explanation
Core idea
மூலம் output கொடுக்கிறது.
01இந்த function
return
02Detail 02
அந்த output தான் next function-க்கு input.
03Flow
1Argument
2Function 1
3Return Value
4Argument to Function 2
5Function 2
6Final Return Value
Common Mistake
Without return problem
01Look · def double(x)
print(x * 2)
02def square(x)
return x * x
print(square(double(5)))
இது problem தரும்.
Why?
03double(5)
10
print செய்கிறது.
04But return value
None
05So composition becomes conceptually
square(None)
06Then
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
Python2 lines
1defget_length(text):2returnlen(text)
Program
Python2 lines
1defdouble(number):2return number * 2
01Composition
double(get_length("Python"))
Works.
02Because
1get_length()returns integer
2double()expects number
Program
Python2 lines
1defget_length(text):2returnlen(text)
Program
Python2 lines
1defdouble(number):2return number * 2
Program
Python1 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
01
Trace
"Python"
↓
get_length()
↓
6
↓
double()
↓
12
Real Life Example
Real time example
01Student result processing · def percentage(mark)
return mark / 100 * 100
02def grade(value) · if value >= 90
return "A"
03elif value >= 75
return "B"
04elif value >= 40
return "Pass"
05else
return "Fail"
print(grade(percentage(82)))
Output
B
Concept
1Mark
2Percentage Function
3Percentage Value
4Grade Function
5Grade
Real Life Example
Better real example
01Suppose total is out of 500 · def percentage(mark)
return mark / 500 * 100
02def grade(p) · if p >= 80
return "A"
03elif p >= 60
return "B"
04elif p >= 40
return "C"
05else
return "Fail"
print(grade(percentage(400)))
Output
A
Dry Run
Object walkthroughFollow creation, available members and the resulting object state1 stage
01
Trace
1400
2400 / 500 × 100
380
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 மட்டும் அல்ல.
01Detail 01
பல functions compose செய்யலாம்.
Program
Example
Python2 lines
1defadd_two(x):2return x + 2
Program
Python2 lines
1defdouble(x):2return x * 2
Program
Python2 lines
1defsquare(x):2return x * x
Program
Python1 lines
1result = square(double(add_two(3)))
Program
Python1 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
Function composition code often nested function calls போல இருக்கும்
f(g(h(x)))
01Execution order
1h(x)
2g(result)
3f(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.
Learning Run Mode — this is the output the author verified, not a live execution.
Output
5
Important
01Here
str()
02
len()
03
print()
04
மூன்றும் functions.
05Composition
print(len(str(12345)))
Real Life Example
Another built in example
Context
print(abs(int("-25")))
01Execution
1"-25"
2int()
3-25
4abs()
525
6print()
725
Explanation
Function composition with lambda
Core idea
Lambda functions-லையும் composition concept பயன்படுத்தலாம்.
01Detail 01
double = lambda x: x * 2
square = lambda x: x * x
02Detail 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
01This
compose(square, double)
02creates conceptually
square(double(x))
03So
1x
2double
3square
04result
Explanation
Higher order function connection
Core idea
functions-ஐ arguments-ஆக receive செய்கிறது.
01இந்த compose()
def compose(f, g):
02Detail 02
So இது Higher-Order Function concept-உடன் தொடர்புடையது.
03Higher-order function என்பது
function-ஐ argument-ஆக receive செய்யலாம்
அல்லது function-ஐ return செய்யலாம்
04Detail 04
Function composition functional programming-ல் முக்கிய concept.
Comparison
Composition vs nested function
Context
இரண்டையும் confuse செய்யக்கூடாது.
Function Composition
f(g(x))
ஒரு function result மற்ற function input.
Nested Function
01def outer() · def inner()
pass
ஒரு function definition மற்ற function-க்குள் இருக்கும்.
இரண்டும் different concepts.
Comparison
Function Composition
Nested Function
Functions combine outputs/inputs
Function inside function definition
f(g(x))
def inner() inside outer
Data flows between functions
Function 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
Python2 lines
1defcount(n):2count(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.
01Example bad design · def process_student(mark)
# percentage calculate
# grade calculate
# message format
# display
02Better concept
1calculate_percentage()
2find_grade()
3format_result()Each function one job.Then compose.
Common Mistake
Error 1
Context
Wrong execution order
01For
f(g(x))
Don't think f() first.
02Correct
g() first
f() next
Common Mistake
Error 2
Context
First function doesn't return value
01def a(x)
print(x + 1)
02def b(x)
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
01def get_name()
return "Python"
02def square(x)
return x * x
03Then
square(get_name())
04tries
"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
01Function composition · O → I
Output → Input
02One function
Output
becomes next function:
Input
Easy Tamil:
ஒரு function கொடுப்பதை அடுத்த function வாங்கும்.
Memory Trick
Another memory trick
For:
f(g(x))
Remember
01
Goes Inside First
02
g() is inside.
03So
g first
04
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
Python2 lines
1result = square(double(5))2Which function executes first inf(g(x))?
Program
Python1 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().
01Reason
is return important in function composition?
02Reason
the output from one function must be available to pass as input to another function.
03Detail 03
Is function composition order-sensitive?
04Detail 04
Yes.
05In general
f(g(x)) ≠ g(f(x))
What is the advantage of function composition?
06Detail 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
01
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
Function composition promotes:
Explanation
Core idea
A. Code repetition
B. Modularity
C. Syntax errors
D. Infinite loops
01Answer
B
List
Function composition and recursion are:
Explanation
Core idea
A. Always same
B. Different concepts
01Answer
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
01Function Composition
02Input
03↓
04Function A
05↓
06Return Value
07↓
08Function B
09↓
10Final Return Value
11Core syntax
12f(g(x))
13Execution
14g(x) first
15↓
16result
17↓
18f(result)
19Most important ideas
20Output of one function
21=
22Input of next function
23f(g(x)) generally ≠ g(f(x))
24Composition depends heavily onreturn values
25And remember
26Function 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.