Imagine for a moment how difficult it would be if you had to write the functionality of the print statement or the random number generator, every time you wanted to use it. In the earliest days of computer programming, this was often the case.

It became apparent, quickly, that programmers needed to create blocks of code that do common functions and then re-use them. We call these blocks of re-usable code functions. In Python, we will use the def statement to define them.

Objectives

Upon completion of this chapter’s exercises, you should be able to:

  • Explain the need for reuse of code in a complex system.
  • Create a function that accepts zero or more values being passed into it.
  • Use default values in the function definition.
  • Use return values from functions in the calling context.
  • Apply the concept of variable scoping when creating and using functions,
  • Apply passed values to a function into a tuple.