OpenSesame
Rapunzel Code Editor
DataMatrix
Support forum
Python Tutorials
MindProbe
Python videos

Functions

Functions are blocks of code that you can call elsewhere in your code. Functions are one of the most important concepts in programming, so let's learn all about them!

In this chapter, you will learn

  • What functions are
  • How to use the def statement to define a function
  • How to pass arguments and keywords to functions
  • About return values
  • About functions that call functions (recursion)

Test yourself

  • Five interactive mini exercises
  • Two review exercises

Functions: what and why?

A function is a re-usable code block, typically with a name. The main goal of functions is to avoid duplication of code. Another important goal of functions is to divide code into parts with clearly defined functions; doing so can drastically improve the readability of your code.

Say that you want to turn a string with multiple words into an acronym, such that 'a coded rendition of names yielding meaning' becomes 'ACRONYM'. You can do that by first splitting the string into separate words, iterating through the resulting list of words, turning the first letter of each word to uppercase, and concatening each first letter to a new string that, in the end, contains the acronym.

words = 'a coded rendition of names yielding meaning'
acronym = ''
for word in words.split():
  acronym += word[0].upper()
print(acronym)

Output:

ACRONYM

So far there is no code duplication, and hence no real need for a function. But now say that you want to do almost the same thing again for another string: 'New York City'. To do this without a function, we would have to copy-paste our code, resulting in code duplication:

words = 'a coded rendition of names yielding meaning'
acronym = ''
for word in words.split():
  acronym += word[0].upper()
print(acronym)
words = 'New York City'
acronym = ''
for word in words.split():
  acronym += word[0].upper()
print(acronym)

Output:

ACRONYM
NYC

When you see this kind of code duplication, then you know that a function is likely to improve your code. Let's see, step by step, how we can accomplish this.

def: defining a function

A function is defined with the def statement, followed by the name of the function and parentheses. Like any statement that is followed by an indented code block, the statement ends with a colon.

Let's define an acronym() function that (for now) determines the acronym for 'a coded rendition of names yielding meaning' and prints out the result. We execute the function by calling its name followed by parentheses.

def acronym():  # Define the function
  words = 'a coded rendition of names yielding meaning'
  s = ''
  for word in words.split():
    s += word[0].upper()
  print(s)


acronym()  # Call the function

Output:

ACRONYM

Mini exercise

(Following-up from the zip() exercise.) Define a function named best_selling_artists that prints out the four best-selling artists of all time and the number of records that they sold, with one line per artist in the following format: 'The Beatles sold 600000000.0 records'.

Arguments and return values

Function arguments

The acronym() function defined above is not very useful, because it lacks flexibility. It determines an acronym but only for the string 'a coded rendition of names yielding meaning', which is hard-coded into the function. To make the function more flexible, you can add arguments (or parameters). That is, you pass one or more variables to the function, and the function then performs some operation on or with these variables.

def acronym(words):
  s = ''
  for word in words.split():
    s += word[0].upper()
  print(s)


acronym('do it yourself')

Output:

DIY

Mini exercise

As before, define a function named best_selling_artists. This time, let the function accept a single argument named artist and print the number of record sales for this artist in the following format: 'The Beatles sold 600000000.0 records'. If the artist is not known, use 0 for the number of record sales. (Tip: use a dict with artists as keys and record sales as values.)

Default function arguments (keywords)

Function arguments can have default values, so that you can, but do not have to, pass these arguments. Such arguments with default values are called keywords. We can redefine acronym() such that words is a keyword that has an empty string as a default value (for which the acronym is also an empty string).

def acronym(words=''):
  s = ''
  for word in words.split():
    s += word[0].upper()
  print(s)


acronym()

Output:


Mini exercise

As before, define a function named best_selling_artists that takes a single argument named artist. The function is identical to that of the previous mini exercise, except that artist should now have a default value of None. If no value for artist is specified when calling the function, the function should ask the user to enter an artist name using the input() function.

Return values

Many functions also have return values; that is, they communicate a value back to where they were called from. We can redefine acronym() such that it doesn't print the acronym directly, but rather returns it.

def acronym(words=''):
  s = ''
  for word in words.split():
    s += word[0].upper()
  return s


ikea = acronym('Ingvar Kamprad Elmtaryd Agunnaryd')
print(ikea)

Output:

IKEA

Mini exercise

As before, define a function named best_selling_artists that takes a single argument named artist with a default value of None. The function is identical to that of the previous mini exercise, except that the number of record sales should now be returned, rather than printed out.

Docstrings

A documentation string, or docstring, is a str that directly follows the def line. This allows you to describe what the function does. In most cases, a docstring will be a multiline string. It is best practice to document all but the most trivial functions with a clear docstring!

def acronym(words=''):
  """Determines the acronym for a string."""
  s = ''
  for word in words.split():
    s += word[0].upper()
  return s

Mini exercise

As before, define a function named best_selling_artists that takes a single argument named artist with a default value of None and returns the number of record sales. Add a docstring to the function that concisely describes what the function does.

Functions that call functions

Most programs consist of functions that call other functions that call yet other functions, etc. For example, we could define a function called acronyms() that takes a list of sentences, where each sentence is itself a string of multiple words, and return a list of corresponding acronyms.

def acronym(words=''):
  """Determines the acronym for a string."""
  s = ''
  for word in words.split():
    s += word[0].upper()
  return s


def acronyms(sentences=[]):
  """Determines the acronyms for a list of strings"""
  l = []
  for words in sentences:
    l.append(acronym(words))
  return l


l = acronyms(['a coded rendition of names yielding meaning',
              'do it yourself',
              'new york city',
              'Ingvar Kamprad Elmtaryd Agunnaryd'])
print(l)

Output:

['ACRONYM', 'DIY', 'NYC', 'IKEA']

Review exercises

Factorial using for

The factorial (!) of a positive integer number is the product of all numbers from 1 up to and including the number itself. So 3! == 3 × 2 × 1. By convention, 0! == 1. The factorial of negative numbers is undefined.

Define a function that takes a number as an argument, and returns the factorial for that number. The function can assume that the input is a non-negative integer. Use a for loop inside the function.

This exercise is not checked automatically, because there are several possible solutions. Click here to see one solution!

Factorial using recursion

When a function calls itself, this is called recursion. Define a factorial function that does not use a for loop, but uses recursion.

This exercise is not checked automatically, because there are several possible solutions. Click here to see one solution!

You're done with this section!

Continue with Modules >>