It keeps happening till the end of the function. It pushes all the new values into the stack when it comes through a recursive call. When we execute a recursion code, the compiler generally uses a stack to store the values like parameter values during each recursive call. Non-tail recursive functions, on the other hand, cannot be optimized by the compiler. These types of functions (Tail Recursive functions) can be optimized and used efficiently by the compiler. As you can see, it only returns a function call and nothing more than that. It can just forget them and concentrate on the recursive function calls. The interpreter need not remember the values of any variables outside the function calls. We need to choose based on the need, and in some cases, we use Recursion for the return values. If not, we will be left with either while and do-while loops or Recursion. When we know the number of iterations, we use a for loop. ![]() When we use loops, we need to know how many times we need to iterate the loop as in a for loop. Loops are used to perform repetitive tasks, and so are the functions? What is the difference, and how to know when to use what? So, we write a function, say factorial for N, in which we call the same function again for N-1, which is repeated till N=1 We need to write a program to find the factorial of a number. Just like this, when we need to write a code for a complex problem, we break it into smaller and repetitive parts. When we want to perform a complex task, we generally break it into smaller tasks doing them one after the other decreases the burden and the complexity. Now, we want to check if 2 is even or odd, so we call the function by giving the argument as 2, which the function will replace at num and the function is carried on it. We defined the function and then wrote the logic in it. Here, evenORodd is the function we defined to check if the given number, num, is an even or an odd number. ![]() ![]() Python Tutorial Python Features Python History Python Applications Python Install Python Example Python Variables Python Data Types Python Keywords Python Literals Python Operators Python Comments Python If else Python Loops Python For Loop Python While Loop Python Break Python Continue Python Pass Python Strings Python Lists Python Tuples Python List Vs Tuple Python Sets Python Dictionary Python Functions Python Built-in Functions Python Lambda Functions Python Files I/O Python Modules Python Exceptions Python Date Python Regex Python Sending Email Read CSV File Write CSV File Read Excel File Write Excel File Python Assert Python List Comprehension Python Collection Module Python Math Module Python OS Module Python Random Module Python Statistics Module Python Sys Module Python IDEs Python Arrays Command Line Arguments Python Magic Method Python Stack & Queue PySpark MLlib Python Decorator Python Generators Web Scraping Using Python Python JSON Python Itertools Python Multiprocessing How to Calculate Distance between Two Points using GEOPY Gmail API in Python How to Plot the Google Map using folium package in Python Grid Search in Python Python High Order Function nsetools in Python Python program to find the nth Fibonacci Number Python OpenCV object detection Python SimpleImputer module Second Largest Number in Python
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |