Return function from function

Introduction:

  • In Python, functions are first-class objects. This means that a function is just like any other object, and you can return a function from another function.

Examples:

  1. A Simple Example to Return a Function:
    • In this example, we define two functions: function1() and function2(). The function1() returns function2() as the return value.
    • Python Program:

      python
      def function1():
          return function2
      
      def function2():
          print('Function 2')
      
      x = function1()
      x()
      
    • Output:

       
      Function 2
      
    • Explanation:
      • function1() returns the reference to function2 without calling it.
      • The returned function reference is assigned to x.
      • Calling x() executes function2().
  2. Return Function - Calculator Example:
    • This example shows a calculator with functions like add(), subtract(), and multiply().
    • Python Program:

      python
      def add(a, b):
          return a + b
      
      def subtract(a, b):
          return a - b
      
      def multiply(a, b):
          return a * b
      
      def getArithmeticOperation(operation):
          if operation == 1:
              return add
          elif operation == 2:
              return subtract
          elif operation == 3:
              return multiply
      
      while True:
          print('Arithmetic Operations')
          print('1. Addition')
          print('2. Subtraction')
          print('3. Multiplication')
          print('0. Exit')
          operation = int(input('Enter the arithmetic operation: '))
          if operation == 0:
              break
          func = getArithmeticOperation(operation)
          a = int(input('Enter a: '))
          b = int(input('Enter b: '))
          result = func(a, b)
          print('The result is:', result)
      
    • Output:

       
      Arithmetic Operations
      1. Addition
      2. Subtraction
      3. Multiplication
      0. Exit
      Enter the arithmetic operation: 1
      Enter a: 58
      Enter b: 4
      The result is: 62
      
    • Explanation:
      • getArithmeticOperation() returns the function based on its argument value.
      • The returned function is then called with the input values a and b.

Exercises:

  1. Return a Function for Different Greetings:
    • Write a function getGreeting() that returns a different greeting function based on the time of day.

      python
      def morning():
          return "Good morning!"
      
      def afternoon():
          return "Good afternoon!"
      
      def evening():
          return "Good evening!"
      
      def getGreeting(time_of_day):
          if time_of_day == "morning":
              return morning
          elif time_of_day == "afternoon":
              return afternoon
          elif time_of_day == "evening":
              return evening
      
      greeting_func = getGreeting("morning")
      print(greeting_func())
      
  2. Return a Math Operation Function:
    • Write a function getOperation() that returns a specific math operation function based on the provided operation name.

      python
      def add(a, b):
          return a + b
      
      def subtract(a, b):
          return a - b
      
      def multiply(a, b):
          return a * b
      
      def getOperation(operation_name):
          if operation_name == "add":
              return add
          elif operation_name == "subtract":
              return subtract
          elif operation_name == "multiply":
              return multiply
      
      op_func = getOperation("multiply")
      print(op_func(3, 4))
      

Summary:

  • In this tutorial, we learned how to return a function from another function with the help of examples. This concept allows for creating flexible and dynamic function calls in your Python programs. Practice writing functions that return other functions to become proficient in this advanced programming technique!