Skip to main content

Section 5.6 Adding new functions

So far, we have only been using the functions that come with Python, but it is also possible to add new functions. A function definition specifies the name of a new function and the sequence of statements that execute when the function is called. Once we define a function, we can reuse the function over and over throughout our program.
Here is an example:
def print_lyrics():
    print("I'm a lumberjack, and I'm okay.")
    print('I sleep all night and I work all day.')
def is a keyword that indicates that this is a function definition. The name of the function is print_lyrics. The rules for function names are the same as for variable names: letters, numbers and some punctuation marks are legal, but the first character can't be a number. You can't use a keyword as the name of a function, and you should avoid having a variable and a function with the same name.
The empty parentheses after the name indicate that this function doesn't take any arguments. Later we will build functions that take arguments as their inputs.

Checkpoint 5.6.1.

    Q-1: What does it means when there are empty parentheses after a function name?
  • It lets you know whether a function is self-defined or included in a Python module.
  • Incorrect! You can find all built-in functions with a quick search. Try again.
  • It means that the function does not return anything.
  • Incorrect! Return statements indicate what a function returns. Try again.
  • It lets you know that nothing will print.
  • Incorrect! A print statement indicates if a function prints something. Try again.
  • It indicates that a function doesn't take any arguments.
  • Correct! Empty parentheses indicate that the function doesn't take any arguments.
The first line of the function definition is called the header; the rest is called the body. The header has to end with a colon and the body has to be indented. By convention, the indentation is always four spaces. The body can contain any number of statements.

Checkpoint 5.6.2.

    Q-2: What is the first line of a function definition called? What is every line after the first line called?
  • body; header
  • Incorrect! The order is off. Where does a header typically go? Try again.
  • title; body
  • Incorrect! Body is correct, but title is not. Try again.
  • header; body
  • Correct! The header is the first line of a function definition and the rest is the body.
  • initialization; body
  • Incorrect! We initialize variables, but functions are defined. Try again.
If you type a function definition in interactive mode, the interpreter prints ellipses (…) to let you know that the definition isn't complete:
>>> def print_lyrics():
...     print("I'm a lumberjack, and I'm okay.")
...     print('I sleep all night and I work all day.')
...
To end the function, you have to enter an empty line (this is not necessary in a script).
Defining a function creates a variable with the same name.
The value of print_lyrics is a function object, which has type “function”.

Checkpoint 5.6.3.

    Q-4: Consider the code below. Which statement is true?
    def printWeather():
        print("It is sunny!")
    
  • The value of printWeather is a function object, which has type "function".
  • Correct! The value of printWeather is a function object, meaning it has the type "function".
  • The value of printWeather is a definition object, which has type "def".
  • Incorrect! The codeblock shows a function definition, but that is not the object/type of printWeather. Try again.
  • The value of printWeather is a def object, which has type "def".
  • Incorrect! Function definitions use the ``def`` keyword, but that is not the object/type of printWeather. Try again.
  • The value of printWeather is a function object, which has type "funct".
  • Incorrect! The value of printWeather is a function object, but its type is not "funct". Try again.
The syntax for calling the new function is the same as for built-in functions:
Once you have defined a function, you can use it inside another function. For example, to repeat the previous refrain, we could write a function called repeat_lyrics and then call that function:
But that's not really how the song goes.

Checkpoint 5.6.4.

Construct a block of code that correctly creates a function called “printMenu” that prints the menu and prices, then call the function. Watch your indentation!

Checkpoint 5.6.5.

The following code should define the function printPrice, that prints items and their prices, and define a second function printReceipt, that uses printPrice to print a receipt. Then, the code should call printReceipt. Watch your indentation!