Calling a function from another function in Python
In this tutorial, we will learn how to call a function from another function in Python.
Let’s focus on the definition of a function.
Certainly, a function consists of a set of statements to perform a specific task.
This is how a function looks like.
def fun(): # function definition print ("Hey u called fun()") fun() # calling a function
Output : Hey u called fun()
The moment fun() is executed,
- The control goes to function definition.
- After, executing that function it returns back.
Call a function from another function in Python
We are going to understand this concept in two ways mainly,
- A sample example to show how it works
- A real-time program to show its usability in programming.
So, in the first step, there are two sample functions namely fun1( ) and fun2( ).
Therefore, we are going to call fun2( ) from fun1( ).
def fun2(): print ("Called by fun1()") def fun1(): # function definition print ("Called by main function") fun2() # calling fun2() from fun1() fun1() # calling a function
Output : Called by main function Called by fun1()
Moreover, the above code shows the possibility of calling many other functions from a function itself.
A program to print all the Armstrong numbers in a given range [a,b]:
While doing the coding for this program, it could be very much clear with the concept of calling a function from another function.
Now, quickly let’s implement it.
A number is said to be Armstrong if and only if,
- The sum of individual digits raised to the power of the number of digits equals the original number.
It seems complex, no it isn’t. Let’s understand by an example.
Take n = 153
Number of digits in n = 3
Therefore sum = (1*1*1) + (5*5*5) + (3*3*3) = 153 [ the digits are cubed as the total digits in n = 3]
As the original number equals the sum, it’s an Armstrong number.
Now moving on to implement the code, by using the concept of calling a function from another function.
def Total_sum(nod,k): s = 0 while(k > 0): r = k % 10 s += (r**nod) # a**b is a raised to power b k //= 10 return s # returns the calculated sum def Number_of_digits(num): # function to calculate number of digits in a number c = 0 while (num>0): c+=1 num//=10 return c def isArmstrong(n): k = n nod = Number_of_digits (k) # calling a Number_of_digits function sum_of_digits = Total_sum (nod,k) # calling Total_sum function from another function isArmstrong() if (sum_of_digits == n): return True return False a = int(input("Enter the lower range :")) b = int(input("Enter the higher range :")) print ("The Armstrong numbers in the given range",a, "and",b,"are") for i in range(a,b+1): if(isArmstrong(i)): print (i)
Output : Enter the lower range : 150 Enter the higher range : 2000 The Armstrong numbers in the given range 150 and 2000 are 153 370 371 407 1634
Hope, the concept is pretty clear and,
This is how the functions come in handy and have more flexibility when they are used to call other functions from their function definition.
You may also read: Call Python function from another Python file