-2

I am having a tough time understanding the concept of recursion in python and would like to understand it as it it a very important topic

tried understanding the concept on youtube and other online websites, expected to learn recursion,but i could not grasp the content properly

4
  • 1
    Recursion, generally, is the act of a function calling itself until some sort of end state is reached Commented Dec 4, 2023 at 16:04
  • I think the main thing is to distinguish function and "function invocation". Such invocation consists of the function itself, the position of execution in the function (what is the next thing to execute in the function) and the values of parameters and local variables. Function invocations are independent from each other. Calling a function (from same function or other) creates a new invocation. Commented Dec 4, 2023 at 16:10
  • I think a good example is the fibonacci series, you can find an explanation here. Commented Dec 4, 2023 at 16:15
  • see: stackoverflow.com/questions/11693819/… Commented Dec 4, 2023 at 16:19

1 Answer 1

0

Recursion means that a function calls itself.

Small example: This function calls itself while counting one up each time it does. If it reaches 10 we stop it or else it calls itself infinitely and runs into an error.

def recursiveFuntion(input):

    # print input value
    print(f'{input=}')

    # stop condition
    if input == 10:
        return
    # recursive call
    else:
        recursiveFuntion(input+1)

# call recursive function
recursiveFuntion(1)
Sign up to request clarification or add additional context in comments.

1 Comment

Using input as the name of one of your variables is a pretty bad idea. You're overwriting the built-in input function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.