Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 5 · Lesson 2 · Core

Parameters, arguments and return values

Pass values in, send a result back with return and keep the difference between print and return clear.

  • 50 minutes
  • Serves O5
  • Assumes M5.L1

Phase 1: Prepare

Step 1 Warm-up recall

From M5.L1: when does a function body run?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • write a function with parameters (Foundation);
  • return a value and use it (Core);
  • tell print from return (Core).

Phase 2: Teach

Step 3 A familiar example

A cashier enters a price and the till works out the price with tax. The price goes in, the amount to pay comes out. That is a function with a parameter and a return value.

Step 4 Clear explanation

A parameter is a name in the brackets of the def. An argument is the actual value you pass in the call. return sends a value back.

def with_tax(price, rate):
    return price + price * rate

total = with_tax(200, 0.1)
print(total)
print(with_tax(50, 0.2))
220.0
60.0
InargumentsFunctionbodyOutreturn value
Values go in as parameters and come out as the returned result.

return ends the function at once and hands the value to the caller. A function with no return gives back None.

CodeWhat happensResult
double(4)returns 88
print(double(4))shows the returned value8
show(4) with only print insideshows 8 but returns NoneNone
x = double(4)stores the returned valuex is 8

Keep print and return apart. Print only shows text on the screen; return gives the value to the program so it can be stored, tested or used in another calculation. Arguments are matched to parameters in order, so with_tax(0.1, 200) would give a very different answer. Be careful with units: a function that expects metres must not be given feet.

Step 5 Worked example (4 marks)

For def area(w, h): return w * h find area(3, 4) + area(2, 5).

  1. area(3, 4) returns 12. (1 mark)
  2. area(2, 5) returns 10. (1 mark)
  3. Add the returned values: 12 + 10 = 22. (1 mark)
  4. Answer: 22. (1 mark)

Go deeper: the professor's notes

A function with a clear name and one job can be depended on by many programs.
Name

Says the job.

Inputs

Parameters.

Output

return.

Ideasmall jobNameclearSharemany usersCarekeep it right
One small job

Impact

Lines of codeFetches in a month
11over 2.4 million

At work: A shared helper

Your team copies a helper into five programs.

  1. Move it to one function.
  2. Test it once.

What a careful practitioner decides. A function others rely on needs a docstring and tests.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. In def f(a, b), a and b are:

2. In the call f(3, 4), 3 and 4 are:

3. A function with no return statement gives back:

4. To use a function result in later code, the function should:

Phase 4: Close

Step 7 Recap

  • Parameters are names; arguments are values.
  • return hands a value back and ends the function.
  • Print shows; return gives.

Step 8 Exit ticket (ungraded)

Write def half(n) that returns n divided by 2 and use it to print half(9).

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Write a function that converts kilograms to pounds and returns the result.

In industry: Ariane 5: four of seven variables protected

The inquiry found that only four of seven critical variables had been protected against overflow, partly to keep the computer within a workload target.

Link to this lesson. Decide on purpose which inputs a function checks, and write that down in its docstring.

Source: Wikipedia, Ariane flight V88, citing the Inquiry Board report

Common mistakes

MistakeWhy it fails
Using print instead of returnThe caller then gets None.
Arguments in the wrong orderThey match parameters by position.
Mixing unitsState the unit in the name or the comment.

Limits. Functions can return several values and take optional parameters; this lesson keeps to the basics.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · What is a parameter?
In the defpriceHoldsa valueUsedin the body
Parameter

A name in the brackets of the def that receives a value, such as price in def with_tax(price, rate).

Card 2 of 3 · What is an argument?
In the call200Matchesby positionOrdermatters
Argument

The actual value you pass when you call, such as 200 in with_tax(200, 0.1). Arguments match parameters by position.

Card 3 of 3 · What does return do?
Endsthe functionGivesa valueCalleruses it
return

It ends the function and hands a value to the caller, so you can store it or use it in another calculation.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards