Prerequisite:Functions in Python
InPython, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples.
What is Calling a Function and a Called Function?
The Function which calls another Function is calledCalling Function and the function which is called by another Function is called aCalled Function.
How does Function Execution Work in Python?
A stack data structure is used during the execution of the function calls. Whenever a function is invoked then the calling function is pushed into the stack and called function is executed. When the called function completes its execution and returns then the calling function is popped from the stack and executed. Calling Function execution will be completed only when Called Function's execution is completed.
In the below figure. The function call is made from theMain function toFunction1. Now the state of the Main function is stored in Stack, and execution of the Main function is continued when Function 1 returns. When Function1 calls Function2, the state of the Function1is stored in the stack, and execution of Function 1 will be continued when Function 2 returns.
Working of Functions when one Function calls another FunctionExamples of a Function Call from Another Function
Let us see a few different examples, that explain a function call from another function in Python.
Example 1:
In this example, theSumOfSquares() function calls theSquare() which returns the square of the number.
Python# Python code to demonstrate calling the# function from another functiondefSquare(X):# computes the Square of the given number# and return to the caller functionreturn(X*X)defSumofSquares(Array,n):# Initialize variable Sum to 0. It stores the# Total sum of squares of the array of elementsSum=0foriinrange(n):# Square of Array[i] element is stored in SquaredValueSquaredValue=Square(Array[i])# Cumulative sum is stored in Sum variableSum+=SquaredValuereturnSum# Driver FunctionArray=[1,2,3,4,5,6,7,8,9,10]n=len(Array)# Return value from the function# Sum of Squares is stored in TotalTotal=SumofSquares(Array,n)print("Sum of the Square of List of Numbers:",Total)
Output :
Sum of the Square of List of Numbers: 385
Example 2:
In this example, we will call a function from another function within the same class. The class methodFunction1() calls the methodFunction2() from theMain class.
Python'''Call a function from within another functionin the same class in Python'''classMain:# constructor of Main classdef__init__(self):# Initialization of the Stringsself.String1="Hello"self.String2="World"defFunction1(self):# calling Function2 Methodself.Function2()print("Function1:",self.String2)returndefFunction2(self):print("Function2:",self.String1)return# Instance of Class MainObject=Main()# Calling Function1Object.Function1()
Output :
Function2: Hello
Function1: World
Example 3:
In this example, we will call the parent class function fromthe child class function. The child class inherits the attributes from the parent class.
Python# Python code to demonstrate calling parent class# method from the child class methodclassParent:# constructor of Parent classdef__init__(self):# Initialization of the Stringsself.String1="Hello"self.String2="World"defFunction2(self):print("Function2:",self.String1)return# Child class is inheriting from Parent classclassChild(Parent):defFunction1(self):# calling Function2 Method in parent classself.Function2()print("Function1:",self.String2)return### Instance of Parent classObject1=Parent()### Instance of Child classObject2=Child()# Calling Function1 using Child class instanceObject2.Function1()
Output :
Function2: Hello
Function1: World