Thumbnail article
Recursive in python
In python, recursion can be made by calling the function inside the function itself. Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. The example of implementation recursive in python is a factorial.
def factorial(n):
   return 1 if (n==1 or n==0) else n * factorial(n - 1);

num = 5;
print("Factorial of",num,"is",factorial(num))
Until the n value of factorial function is 0, the factorial function will call its function. Another example code of recursive is the countdown program below.
def countdown(n):
    print(n)
    if n > 0 : countdown(n - 1)

countdown(5)
You can see on both of the program. The program called their own function. Recursion is a process or procedure of a function that calls itself repeatedly. Because the process in this recursive occurs repeatedly, there must be a condition that limits the repetition, otherwise the process will never stop until the memory used to accommodate the process can no longer accommodate.The advantage of recursive functions is that the program becomes shorter. In some cases, it is easier to use recursive functions, for example: exponents, factorials, and Fibonacci, and some other sequence processes. Recursive functions are more efficient and faster than iterative processes. The disadvantage of recursive functions is that it takes up more memory, because each part of itself is called, it will require a certain amount of memory space for storage. Recursive often does not stop so memory will be used up and the program may hang. In some situations, both recursive and iterative solutions have their advantages and disadvantages. It is quite difficult to determine which is the simplest, clearest, most efficient and easiest of all. It can be said that the choice of iterative and recursive methods is depends on the context of the problem to be solved according to the abilities concerned.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *