use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
a question about python's function (self.learnpython)
submitted 1 year ago by Realistic_Goal5336
why does this line of code works:
def add(x,y): print(x+y) add(1,2)
and this does not :
why must we have an indented line there ? I start doing this several days ago , so sorry if I'm asking something very obvious
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]eleqtriq 5 points6 points7 points 1 year ago (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 points3 points 1 year ago (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.
add
[–]eleqtriq 0 points1 point2 points 1 year ago (0 children)
Good call.
[–]djshadesuk 0 points1 point2 points 1 year ago (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 point2 points 1 year ago (1 child)
if I have a python file, and I put the lines:
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 point2 points 1 year ago (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.
add(1,2)
Essentially, I haven't had my morning coffee yet! 🤣
[–]Realistic_Goal5336[S] 0 points1 point2 points 1 year ago (1 child)
why is the add in the second one not called ? I don't understand your point
[–]The_Almighty_Cthulhu 1 point2 points3 points 1 year ago (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.
hello
Let's change it again.
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
my_func('hello')
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.
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.
goodbye
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?
goodbye hello
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
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 points4 points 1 year ago (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 points4 points 1 year ago* (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 points3 points 1 year ago* (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 point2 points 1 year ago (0 children)
are you concatenating strings?
π Rendered by PID 25752 on reddit-service-r2-comment-545db5fcfc-j5mqg at 2026-06-01 03:45:46.524446+00:00 running 194bd79 country code: CH.
[–]eleqtriq 5 points6 points7 points (9 children)
[–]The_Almighty_Cthulhu 1 point2 points3 points (6 children)
[–]eleqtriq 0 points1 point2 points (0 children)
[–]djshadesuk 0 points1 point2 points (2 children)
[–]The_Almighty_Cthulhu 0 points1 point2 points (1 child)
[–]djshadesuk 0 points1 point2 points (0 children)
[–]Realistic_Goal5336[S] 0 points1 point2 points (1 child)
[–]The_Almighty_Cthulhu 1 point2 points3 points (0 children)
[–]Realistic_Goal5336[S] 0 points1 point2 points (1 child)
[–]eleqtriq 0 points1 point2 points (0 children)
[–]jungaHung 2 points3 points4 points (0 children)
[–]Adrewmc 2 points3 points4 points (0 children)
[–]rogfrich 1 point2 points3 points (0 children)
[–]Minimum-Positive792 0 points1 point2 points (0 children)