Lessons · Python · calling a function
Functions hand something back
Most functions do a job and then give you a result. You have to catch the result or it is thrown away.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Nearly all real work is calling something that already exists and using what comes back. Reading a file, fetching a price, counting rows. If you drop the result you did the work and threw it away.
How to think about it
Ask two things of anything you call: what does it need from me, and what does it hand back? If you cannot answer the second, look it up before writing the next line.
Worked example
len("cat")len counts the characters and hands back 3.size = len("cat")Catch that 3 in a name so you can use it.print(size)Prints 3. Without the line above, the 3 existed for an instant and was discarded.
Your turn
Count the characters in "elephant" and print the number.
size = ("elephant")
print(size)Solve one with the tests running
The trap
Writing len("cat") on a line by itself does nothing visible. The answer was produced and dropped. This is the single most common beginner surprise.