38 lines
988 B
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
Recursion happens when control enters a loop that has no exit. This can happen when a method invokes itself or when a pair of methods invoke each other. It can be a useful tool, but unless the method includes a provision to break out of the recursion and ``++return++``, the recursion will continue until the stack overflows and the program crashes.
2020-06-30 12:48:07 +02:00
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:48:07 +02:00
----
def my_pow(num, exponent): # Noncompliant
num = num * my_pow(num, exponent - 1)
return num # this is never reached
----
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:48:07 +02:00
----
def my_pow(num, exponent): # Compliant
if exponent > 1:
num = num * my_pow(num, exponent - 1)
return num
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]