What is Implicit recursion? – GeeksforGeeks


Improve Article

Save Article

Like Article

Improve Article

Save Article

What is Recursion?

Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another condition is met.

What is Implicit Recursion?

A specific sort of recursion called implicit recursion occurs when a function calls itself without making an explicit recursive call. This can occur when a function calls another function, which then calls the original code once again and starts a recursive execution of the original function. This can sometimes be difficult to spot and can lead to unintended behavior if not handled carefully.

Example of the implicit recursion:

We will use implicit recursion to find the second-largest elements from the array:

Python3

def find_largest(numbers):

    largest = numbers[0]

    for number in numbers:

        if number > largest:

            largest = number

    return largest

  

  

def find_second_largest(numbers):

  

    

    numbers.remove(find_largest(numbers))

  

    

    return find_largest(numbers)

  

  

  

numbers = [1, 2, 3, 4, 5]

  

second_largest = find_second_largest(numbers)

print(second_largest)

In this case, the find_second_largest( It’s crucial to remember that explicit recursion could also be used to accomplish this example, which would make the code simpler to read and comprehend. ) method calls the find_largest() function via implicit recursion to locate the second-largest number in a provided list of numbers. Implicit recursion can be used in this way to get the second-largest integer without having to write any more code, which is a handy use.

Problem with implicit recursion:

In the case of the implicit recursion, the issue of an infinite function call may occur. Here is a case where implicit recursion can cause the problem:

Python3

def func1():

    print("This is the first function")

    func2()

  

def func2():

    print("This is the second function")

    func1()

  

func1()

Output: 

This is the first function
This is the second function
This is the first function
This is the second function
...
...

In this example, the func1() function calls func2(), which then calls func1() once again.

Steps to avoid this issue:

  • When utilizing implicit recursion, caution must be taken since if the recursive calls are not handled correctly, it is simple to produce an endless loop
  • Additionally, it’s typically recommended to avoid implicit recursion if feasible because it might make your code more challenging to read and maintain. 
  • Instead, explicit recursion, which is simpler to read and understand, is typically preferred.

Related Articles:


Improve Article

Save Article

Like Article

Improve Article

Save Article

What is Recursion?

Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another condition is met.

What is Implicit Recursion?

A specific sort of recursion called implicit recursion occurs when a function calls itself without making an explicit recursive call. This can occur when a function calls another function, which then calls the original code once again and starts a recursive execution of the original function. This can sometimes be difficult to spot and can lead to unintended behavior if not handled carefully.

Example of the implicit recursion:

We will use implicit recursion to find the second-largest elements from the array:

Python3

def find_largest(numbers):

    largest = numbers[0]

    for number in numbers:

        if number > largest:

            largest = number

    return largest

  

  

def find_second_largest(numbers):

  

    

    numbers.remove(find_largest(numbers))

  

    

    return find_largest(numbers)

  

  

  

numbers = [1, 2, 3, 4, 5]

  

second_largest = find_second_largest(numbers)

print(second_largest)

In this case, the find_second_largest( It’s crucial to remember that explicit recursion could also be used to accomplish this example, which would make the code simpler to read and comprehend. ) method calls the find_largest() function via implicit recursion to locate the second-largest number in a provided list of numbers. Implicit recursion can be used in this way to get the second-largest integer without having to write any more code, which is a handy use.

Problem with implicit recursion:

In the case of the implicit recursion, the issue of an infinite function call may occur. Here is a case where implicit recursion can cause the problem:

Python3

def func1():

    print("This is the first function")

    func2()

  

def func2():

    print("This is the second function")

    func1()

  

func1()

Output: 

This is the first function
This is the second function
This is the first function
This is the second function
...
...

In this example, the func1() function calls func2(), which then calls func1() once again.

Steps to avoid this issue:

  • When utilizing implicit recursion, caution must be taken since if the recursive calls are not handled correctly, it is simple to produce an endless loop
  • Additionally, it’s typically recommended to avoid implicit recursion if feasible because it might make your code more challenging to read and maintain. 
  • Instead, explicit recursion, which is simpler to read and understand, is typically preferred.

Related Articles:

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@technoblender.com. The content will be deleted within 24 hours.
GeeksForGeeksImplicitRecursionTechTechnologyUpdates
Comments (0)
Add Comment