r/AskProgramming • • Jun 05 '26

Help beginner

What is difference between output and return. I have seen some videos and it says like return is for computer and output is for human. Is it like two seperate ways one is displayed and one is stored? If so then wouldn't it become same like a stored variable?

1 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/Fun-Ship-2026 Jun 05 '26 edited Jun 05 '26

So I did some chatgpt to get some example of my understanding can you clarify if I am correct.

When Python sees:

def add():

result = 2 + 3

return result

it remembers that calling add() means "run the code inside this function." So when you write:

x = add()

1.Python does this:

2.See add()

3.Run the function body

4.Compute 2 + 3

5.Execute return result

6.Replace add() with the returned value (5)

So Python effectively treats: Python

x = add()

as if it became:

x = 5

after the function finishes. The parentheses () are empty because the function doesn't need any inputs (parameters). It already knows to add 2 and 3.

For example:

def add():

return 2 + 3

print(add())

is roughly like:

print(5)

after the function runs. The important idea is that a function call is an expression that evaluates to its return value. That's why add() can appear on the right side of = even though nothing is inside the parentheses. If you removed return:

def add():

result = 2 + 3

x = add()

print(x)

then x would be:

None because the function didn't return a value

1

u/AFK_MIA Jun 05 '26

Yes, I think you've got it - except at the very end. print(x) doesn't change the value of x if it's passed as an argument to the function. if the line were x = print(x), then x would get changed to None since print doesn't return a value, but it would print the number 5 to the terminal.