Chapter 20 : Functions can return something

what = add( age, subtract(height, multiply(weight, divide(iq, 2))))

this was the last last part of the code is

add subtract multiply and divide were the functions defined in the first part of the script

add( age, subtract(height, multiply(weight, divide(iq, 2))))

this is a function inside function but each of the four functions have a print part which shows what it is doing why is that part not turning into a string for the composed function and causing it to not work?

Do you mean, why doesn’t this happen?

def print_test(number):
   print("Your number is:", number)
   return number * 33

x = print_test(100)
# do you think this prints:  Your number is: 100

It’s a common misunderstanding to think that print is the same as return. I actually can’t figure out why people think this so if you can shed some light on your thinking in this case that’d help me explain it better. The print() only writes text to the screen. That’s all. It does not alter any variable in your program at all.

The return however is what takes a value and returns it from the function, so that it can be assigned to a variable. They are two separate and completely different things:

print takes characters and writes them to a terminal.
return lets the function have a value you can assign

Let me know if that explains it.

A free service run by Zed A. Shaw for