Basics of recursion (Recursion)
What is Recursion?
Some programming languages allow a module or function to be called to itself. This technique is called Recursion. In recursion, a function can: call this function directly or call a function b that returns the call to the original function a . Function a is called recursive function.
For example , a function calls itself
int function ( int value ) { if ( value < 1 ) return ; function ( value - 1 ); printf ( "%d " , value ); }
For example , a function that calls another function returns a call to the original function
int function ( int value ) { if ( value < 1 ) return ; function ( value - 1 ); printf ( "%d " , value ); }
Characteristics of recursive functions
A recursive function can continue to happen countless times like an infinite loop. To avoid this, you must keep in mind the following two properties of the recursive function:
Basic condition : there must be at least one condition for when this condition is met, calling that function (called recursion) will stop.
Proximity: whenever the recursive function is called, it is asymptotic to the basic condition.
Recursive function deployment
Many programming languages implement recursion in the manner of stacks. In general, whenever a function (caller function) calls another function ( called function - callee ) or calls itself (callee), the caller function transmits the control to the callee. This transfer process may also include some data from caller to callee.
Compare recursion and loop
One might say that why using recursion while using a loop can do the same thing. The first reason is that recursion makes the program more readable and with today's improved CPU systems, recursion is much more efficient when compared to loops.
Time complexity of recursive function
With loops, we take the number of loops to calculate the time complexity. Similar to recursion, assuming everything is constant, we calculate the time that a recursive call is created. A call made to a function will be Ο (1), so for n is the time a recursive call is made, the time complexity of recursive function will be Ο (n).
Memory complexity (complexity space) of recursive function
Memory complexity is estimated based on the amount of additional memory required for a module to be executed. With loops, the compiler almost doesn't need any extra memory. The compiler will update the value of the variable used right in the loop. But with recursion, the system needs to store dynamic logs every time a recursive call is made. Therefore it can be said that the memory complexity of the recursive function is higher than the loop.
According to Tutorialspoint
Previous article: Heap data structure
Next article: Problem of Hanoi Tower (Tower of Hanoi)
You should read it
May be interested
- Fibonacci series in Data Structures and Algorithmsthe fibonacci sequence creates numbers by adding two numbers in front. fibonacci series start from two numbers: f0 & f1. the initial value of f0 & f1 may be 0, 1 or 1, 1, respectively.
- Git and GitHub - Using Git the right way to maximize your worktoday's lesson we will learn the basics of git and github
- How to fix CPU working 100%this article will list the basics that cause the cpu to work 100% and how to fix it.
- Kubernetes Cluster Basicsclusters are the main power of kubernetes: the ability to schedule and run containers across a group of machines, either on a physical system or in the cloud.
- Things you need to know about 10Gif you follow the technology news, you probably heard about 5g, the next generation of wireless technology for mobile networks, but you might not know about 10g broadband, advertised at the show. ces this year.
- TCP and IP optionsgoing back to the basics is always a good idea. one of the most basic parts of computer communication knowledge is four basic protocols: ip, tcp, udp and icmp.
- 5 issues for enterprise securitysometimes computer users forget the basics of security and create a hole in the process.
- How to Wrap Books As a Giftwhether you've picked out a thriller for your mystery-loving friend or a romance novel for your sappy sibling, books are often great gifts for loved ones. the wrapping basics are pretty simple, and if you want, you can elevate the...
- How to Use Scratchscratch is a great educational tool developed by mit. it enables nearly anyone to experiment with the basics of vector art, animation, and game development. you can create projects by yourself, or you can collaborate with other creators...
- Network basics: Part 13 - Creating groupsin the previous article of this series, i showed you how to use active directory users and computers console to create and manage user accounts. in this section, we want to continue introducing you to groups.