How to Print the Output of a Function?


#1

I’m drawing a blank on how to print the output of a function?

def distance_from_zero(distance):
if type(distance) == int or type(distance) == float:
return abs(distance)
else:
return “Nope”
distance_from_zero(-321)
print distance_from_zero

This just tells me the …location? of the function distance_from_zero?


FAQ: Learn Python: Functions - Review: Built-In Functions
#2

here:

distance_from_zero(-321)

you call the function correctly, but you didn’t put a print statement before the function call. If you would have put a print statement, it would print the returned result

here:

print distance_from_zero

calling the function requires parentheses. Without the parentheses, you will just get a reference of the function


#3

Thanks for the reply.
But just wondering, on the second half of your reply, don’t you need some input or is there some way to print whats “stored” in a function from it’s return without executing the function?

for example I tried this first after reading that second part out of curiosity.

print distance_from_zero()

but I get

TypeError: distance_from_zero() takes exactly 1 argument (0 given).

So I would always have to

print distance_from_zero(value)

instead of say printing what the function may have resolved earlier in the code?
Hope that makes sense.


#4

print distance_from_zero i literally copied this from your code, print distance_from_zero won’t execute the function, it just give you information about the function like memory address. Its just a reference.

no, this is not possible.

lets make an analogy, if i give you a task (lets say cleaning your room) to do later (which represents a function), i only can see the result of the cleaning after you preformed the task.

the return in this analogy would be you come back to me after you task is done, and tell me the task is done


#5

I meant this part of your reply

But going with that analogy, say you asked me to clean my room later for …30 minutes.
And I “return” to you and say I cleaned for 30 minutes.

And you wanted to tell the world later (print) that I did that.
Does that mean you have to tell me to clean my room again for 30min through the print function?

I understand how to fix my initial code with what you’ve explained. I’m just trying to explore how this stuff works.


#6

reference can be useful, lets say a built-in function like abs:

print abs(-273)

that gives you the absolute value. But we can also store the reference in a different variable:

a = abs
print a(-273)

now a is the abs function, can be useful

time is totally irrelevant in the analogy, it doesn’t fit. A function is just a task which might need doing, but it might not be done at all. (if there are no function calls to that particular function)

my room cleaning analogy might not be perfect for explaining the print part

lets say i make you a math assignment (3+3, whatever), then you write the answer (6, obviously) on a piece of paper, hand the piece of paper back to me (return) then i read the answer out loud (print)