Python Practice › Function Programs › Program 14
Function inside a function
Write a Python program that defines and uses a function inside another function.
Output
Hello, Asha!
Approach
- The inner function is only visible inside the outer one; it can read the outer variables.
Solution
def greet(name): def make_message(): return f"Hello, {name}!" return make_message() print(greet("Asha"))Output
Hello, Asha!
Line by line
Lines 2-3
make_message reads name from the enclosing function.
Learn the concept
New to this topic? Read the lesson, then come back.