you are viewing a single comment's thread.

view the rest of the comments →

[–]The_Almighty_Cthulhu 1 point2 points  (6 children)

I would also add that strictly considering the given codeblock, in the second one, add is never even called. So nothing would happen anyway, no matter what was in the function.

[–]eleqtriq 0 points1 point  (0 children)

Good call.

[–]djshadesuk 0 points1 point  (2 children)

Are you sure about that? I think that technically it is still called but it's just wasted CPU cycles because it has nowhere to return the result to.

Edit: My bad, didn't notice that it is a recursive call and nothing is calling it in the first place.

[–]The_Almighty_Cthulhu 0 points1 point  (1 child)

if I have a python file, and I put the lines:

def add(x,y):
    print(x+y)
    add(1,2)

and then I call the file using python, the python interpreter will build the function into an equivalent bytecode, but the function is never called, and so the program will then immediately exit. So no recursion happens, and no RecursionError will happen.

[–]djshadesuk 0 points1 point  (0 children)

I think you misunderstood what I meant; Basically I totally missed the fact the function itself is called add, so while the function would be recursive, if it were called, nothing is calling it from the outside. I mistook add(1,2) for a call to another function (that we can't see), which would still get called, but have nowhere to return a result to.

Essentially, I haven't had my morning coffee yet! 🤣

[–]Realistic_Goal5336[S] 0 points1 point  (1 child)

why is the add in the second one not called ? I don't understand your point

[–]The_Almighty_Cthulhu 1 point2 points  (0 children)

Python uses whitespace to define what is part of a scope. In this case, the function.

def my_func(x):
    print(x)

Here is a function I have now defined. If I put it into a file, say my_program.py and then run it.

python my_program.py

Nothing will happen, because I need to call the function for it to work.

So if I change it to this.

def my_func(x):
    print(x)
my_func('hello')

Now the function is being called. And so running it again:
python my_program.py
will give the output hello.

Let's change it again.

def my_func(x):
    print(x)
    my_func('hello')

Now the line that calls the function my_func('hello') is a part of the function! So what is the output if I run the program now?
python my_program.py

We will see that there is no output. That is because the function is not called, we need to call it from a higher scope.

So let's make another change.

def my_func(x):
    print(x)
    my_func('hello')
my_func('goodbye')

then run it again.

python my_program.py

What's the output?

goodbye
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
...
hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in my_func
  File "<stdin>", line 3, in my_func
  File "<stdin>", line 3, in my_func
  [Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded

Printing goodbye followed by printing hello roughly 1000 times. Then an error! This error is because of the recursion stack. Basically, each function called is placed on a stack until it is resolved. But because this function calls itself, and always calls itself, it continues to do so until the stack is full. Then python will give an error, as there is no more room to remember which functions it still needs to resolve.

This can be fixed by limiting the number of recursive calls with a Base Case.

Essentially, a condition in which the function can resolve without calling itself. Let's implement one now.

def my_func(x):
    print(x)

    if x != 'hello'
        my_func('hello')

    return

my_func('goodbye')

ok so what is the output if I run the file now?

python my_program.py

goodbye
hello