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 Show So is it possible to call a definition/function from within itself?
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
For more detail, visit the github gist that was used for this answer
answered Mar 16, 2015 at 9:21
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
View Discussion Improve Article Save Article View Discussion Improve Article Save Article Prerequisite: Functions in Python Calling and Called Function ? How does Function execution work? 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. Consider the below Example of the function call. The Function SumOfSquares function calls the Function Square which returns the square of the number. Python3
Output : Sum of the Square of List of Numbers: 385 Calling Function From another Function within Same class – Python3
Output : Function2 : Hello Function1 : World Calling parent class Function from Child class Function – Python3
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.
|