You are on page 1of 1

HEAD RECURSION

For instance,

def fun(n):

if(n > 0):

fun(n – 1)

print(n, end=””)

print(fun(3)

Output: 123

Explanation:

In this case, the function is called with the argument of 3, which is greater than 0. The
function then calls itself again with the argument of 2, followed by 1, and then 0. Each time the
function is called, the value of n is printed. When the condition of n > 0 is no longer met (n = 0), the
function stops calling itself and the statement print(fun(3)) is executed, which prints out the values
of n from 3 to 1 in descending order, resulting in 1 2 3.

You might also like