Week 7 of 106 hours5 exercises

Week 7: Functions: reusable building blocks

Package code into functions with parameters and return values, and learn how names and scope work.

By the end of this week you can

  • Define and call functions
  • Use parameters, defaults and keyword arguments
  • Return values and know the difference from print
  • Understand local and global names

Defining and calling a function

A function is a named block of code you can run whenever you like. You define it with def, give it a name and a list of parameters in brackets, end the line with a colon and indent the body. Defining a function does not run it; calling it does.

Functions stop you repeating yourself and let you give a clear name to an idea, such as is_prime or total_price. A good function does one job.

name is a parameter. The value you pass in a call, such as "Asha", is called an argument.

return versus print

print shows something to a person. return hands a value back to the code that called the function, so it can be stored or used in more maths. A function with no return gives back None.

When Python reaches a return it leaves the function straight away. A function can return several values at once; they arrive as a tuple that you can unpack.

Because area returns a value, we can add two calls together. A function that only printed could not do that.

Default and keyword arguments

A parameter can have a default value, used when the caller leaves it out. Give arguments by name (keyword arguments) to make calls easier to read and to skip some defaults. Parameters with defaults must come after those without.

Never use a list or dictionary as a default value; Python creates it only once, and every call would share it. Use None and create the list inside the function.

Each call to add_item gets its own fresh list because we create it inside the function.

Scope: where a name lives

A name created inside a function is local: it exists only while the function runs and is invisible outside. Parameters are local too. A function can read a name defined outside, but to change an outer variable you would need the global keyword. In practice, prefer passing values in as arguments and sending results out with return; code written that way is much easier to test.

Docstrings, a string on the first line of the function body, describe what it does. They cost nothing and help everyone, including you in three months.

Try removing the # from print(tax) to see the NameError.

Common mistakes

  • Printing inside a function when you meant to return, then being unable to use the result.
  • Forgetting to call the function. greet without brackets is the function itself, not a call.
  • Using a list or dictionary as a default value.
  • Writing code after a return in the same block. It never runs.

Quick recap

  • def defines a function; calling it runs it.
  • return gives a value back; print only shows it.
  • Defaults and keyword arguments make calls flexible and readable.
  • Names made inside a function are local to it.

Practise this week

Work through the 5 exercises for Week 7. Each one tells you which tests pass, and has a hint and a solution if you get stuck.

Open Week 7 exercises

Check your understanding

Pick an answer for each question, then press Check answers.

  1. What does a function return if it has no return statement?
  2. Which defines a parameter with a default value?
  3. def f(): y = 5. After calling f(), what happens with print(y) outside?
  4. What is the difference between print and return?
  5. What does the call greet(name="Asha") use?

This course is for learning. The Python engine runs in your browser and your code is never sent to us.