Module 4 - Section 1 - While and Functions

4.1.7 - More about functions

  • Once you return a value from a function, it immediately stops being executed. Any code after the return statement will never be executed.

Copy the following code to Thonny script editor and save it as add3Numbers :


At the CLI area write

>>> print(addNumbers(4, 5, 19))

As you can see the function returns the value and the print command after the return does not execute.

  • In programming, a docstring is a string literal specified in source code that is used, like a comment, to document a specific segment of code. Unlike conventional source code comments, docstrings are not stripped from the source tree when it is parsed, but are retained throughout the runtime of the program. This allows the programmer to inspect these comments at run time, for instance as an interactive help system.

The next function returns the quotient and the remainder of the Euclidian Division. Copy the script to Thonny script editor and save it as euclidianDivision.


Between the 2nd and the 7th line of the function there is the documentation of the function. As stated this documentation is not ignored by python. After execution of the script, we can test it.

>>> print(division(8,3))

>>> print(division(47,4))

The results is something like a list (called tuples) and we can print them separately the same way that we can print items of a list:

>>> print(division(47,4))[0] # The first item of the tuple, the quotient

>>> print(division(47,4))[1] # The second item of the tuple, the remainder

Also we can print the documentation of the function.

>>> print(division.__doc__)

This is extremely useful when we inspect someone else's scripts or we forgot what our code supposed to do!!