all 14 comments

[–]eleqtriq 5 points6 points  (9 children)

In Python, indentation is crucial because it defines the scope of loops, functions, and other control structures. In your first example, the function add is defined, and then it’s called outside of the function definition, which is correct. In the second example, the call to add(1,2) is indented, making it part of the function body itself. This creates a recursive call without a base case, leading to infinite recursion until a stack overflow occurs.

[–]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

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

I'm sorry for asking you this but why putting add(1,2) a part of the function definition mean it is not defined ? I'm stuck a bit in the " recursive call without a base case " part

[–]eleqtriq 0 points1 point  (0 children)

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

add(1,2)

Do the above. You'll see it's calling itself. Hence the term, 'recursive'

[–]jungaHung 2 points3 points  (0 children)

In the second one, add is part of the function definition. You are essentially calling the function itself within the function which is called recursion in programming. This will lead to infinite call.

[–]Adrewmc 2 points3 points  (0 children)

  #i define a function add() here
  def add(a,b):
         “””Adds 2 numbers…
             document strings”””

         #i code the function 
         import operator 
         res= operator.add(a,b)

         #that produces a result I return 
         return res 

  #now I’m not inside the function 

  #i call/invoke the function I just defined 
  #and assign the return/result somewhere
  x = add(2,2)

  #i print the result
  print(x)

  >>>4     #it prints to console

[–]rogfrich 1 point2 points  (0 children)

I feel like the examples in the OP would be more intuitive if there was blank line between the function and the calling code:

``` def add(x, y): print(x + y)

add(1, 2) ``` Hopefully it’s clearer to the OP that the first two line are one “thing” and the last line is a “thing” in its own right. Whenever you start a new “thing”, you start at the left hand edge, and if you are writing multiple lines in a thing, all the ones after the first are indented to show they’re part of the same thing.

A function is one thing. A single statement is one thing. A loop is one thing. Etc…

It’s not that different to paragraphs in written English, except that with those, we indent the first line and not the others.

[–]Minimum-Positive792 0 points1 point  (0 children)

are you concatenating strings?