Can i call a function inside the same function in python?

The code I already have is for a bot that receives a mathematical expression and calculates it. Right now I have it doing multiply, divide, subtract and add. The problem though is I want to build support for parentheses and parentheses inside parentheses. For that to happen, I need to run the code I wrote for the expressions without parentheses for the expression inside the parentheses first. I was going to check for "(" and append the expression inside it to a list until it reaches a ")" unless it reaches another "(" first in which case I would create a list inside a list. I would subtract, multiply and divide and then the numbers that are left I just add together.

So is it possible to call a definition/function from within itself?

Can i call a function inside the same function in python?

MackM

2,7975 gold badges31 silver badges43 bronze badges

asked Mar 16, 2015 at 9:13

Yes, this is a fundamental programming technique called recursion, and it is often used in exactly the kind of parsing scenarios you describe.

Just be sure you have a base case, so that the recursion ends when you reach the bottom layer and you don't end up calling yourself infinitely.

(Also note the easter egg when you Google recursion: "Did you mean recursion?")

answered Mar 16, 2015 at 9:14

Daniel RosemanDaniel Roseman

574k61 gold badges839 silver badges852 bronze badges

1

Yes, as @Daniel Roseman said, this is a fundamental programming technique called recursion.

Recursion should be used instead of iteration when you want to produce a cleaner solution compared to an iterative version. However, recursion is generally more expensive than iteration because it requires winding, or pushing new stack frames onto the call stack each time a recursive function is invoked -- these operations take up time and stack space, which can lead to an error called stack overflow if the stack frames consume all of the memory allocated for the call stack.

Here is an example of it in Python

def recur_factorial(n):
   """Function to return the factorial of a number using recursion""" 
   if n == 1:
      return n
   else:
      return n*recur_factorial(n-1) 

For more detail, visit the github gist that was used for this answer

answered Mar 16, 2015 at 9:21

Can i call a function inside the same function in python?

Jameel GrandJameel Grand

2,20615 silver badges31 bronze badges

1

yes it's possible in "python recursion" and the best describe is: "A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively"

answered Dec 17, 2021 at 8:08

Can i call a function inside the same function in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisite: Functions in Python
    In Python, 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 help of multiple examples. 

    Calling and Called Function ? 
    The Function which calls another Function is called Calling Function and function which is called by another Function is call Called Function.

    How does Function execution work? 
    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 is execution completes.

    In the below figure. The function call is made from the Main function to Function1, Now the state of the Main function is stored in Stack, and execution of the Main function is continued when the Function 1 returns. The Fucntion1 Calls Function2 now the State of the Function1 is stored stack and execution of Function 1 will be continued when Function 2 returns. 

    Can i call a function inside the same function in python?

    Consider the below Example of the function call. The Function SumOfSquares function calls the Function Square which returns the square of the number. 

    Python3

    def Square(X):

        return (X * X)

    def SumofSquares(Array, n):

        Sum = 0

        for i in range(n):

            SquaredValue = Square(Array[i])

            Sum += SquaredValue

        return Sum

    Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    n = len(Array)

    Total = SumofSquares(Array, n)

    print("Sum of the Square of List of Numbers:", Total)

    Output : 

    Sum of the Square of List of Numbers: 385 

    Calling Function From another Function within Same class –
    In the below example, the class method Function1 calls method Function2 from the class.

    Python3

    class Main:

        def __init__(self):

            self.String1 ="Hello"

            self.String2 ="World"

        def Function1(self):

            self.Function2()

            print("Function1 : ", self.String2)

            return

        def Function2(self):

            print("Function2 : ", self.String1)

            return

    Object = Main()

    Object.Function1()

    Output : 

    Function2 :  Hello
    Function1 :  World

    Calling parent class Function from Child class Function –
    Consider the below example the child class method invokes the parent class method. The child class inherits the attributes from the parent class.

    Python3

    class Parent:

        def __init__(self):

            self.String1 ="Hello"

            self.String2 ="World"

        def Function2(self):

            print("Function2 : ", self.String1)

            return

    class Child(Parent):

        def Function1(self):

            self.Function2()

            print("Function1 : ", self.String2)

            return  

    Object1 = Parent()

    Object2 = Child()

    Object2.Function1()

    Output : 

    Function2 :  Hello
    Function1 :  World

    Can we call a function inside the same function in Python?

    Python also accepts function recursion, which means a defined function can call itself. Recursion is a common mathematical and programming concept. It means that a function calls itself.

    How do you call the same function in the same function?

    Answer 53e550d39c4e9d53dc000182 Calling a function inside of itself is called recursion. It's a technique used for many applications, like in printing out the fibonacci series.