Function inside a function

EasyFunction Programs · 14 of 39

Write a Python program that defines and uses a function inside another function.

Advertisement
Output
Hello, Asha!

Approach

  1. The inner function is only visible inside the outer one; it can read the outer variables.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs