Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.

This is the same set of maths sub programs you met in the previous task, with one of them changed. Run it once before you start, so that you know what it does, and then look at it closely.

def maths1():
    num1 = 50
    num2 = 5
    return num1 + num2

def maths2():
    num1 = 50
    num2 = 5
    return num1 - num2

def maths3(num1, num2):
    return num1 * num2

output_num = maths2()
print(output_num)

How many functions are there in the code?

Answer

Three: maths1, maths2 and maths3.

There are three def lines, so there are three sub programs, and all three of them are functions.

How do you know that they are functions, not just sub programs?

Answer

Because every one of them ends with a return.

A sub program is any set of actions with a name. A function is a sub program that sends a piece of data back to the main program when it has finished, and return is the only way to do that. Every function is a sub program; only the ones with a return are functions.

Look for the word return in the indented body. maths1 returns num1 + num2, maths2 returns num1 - num2 and maths3 returns num1 * num2. Three return lines, three functions.

A sub program like say_hi, whose body is only a print, is not a function. It puts something on the screen, but it hands nothing back to the code that called it.

Which functions have parameters? How can you tell?

Answer

Only maths3, and it has two of them.

You can tell by looking inside the brackets on the def line. maths3 is defined as def maths3(num1, num2):, so it expects to be given two pieces of data, and it uses the names num1 and num2 for them. The brackets of maths1 and maths2 are empty, so neither of them expects anything from outside.

That is the difference between the two styles in this program. maths1 and maths2 create num1 and num2 for themselves, inside their own bodies, so they always work with 50 and 5 and always give the same answer. maths3 is handed its two numbers by whoever calls it, so it can multiply any pair of numbers at all.

Watch the brackets rather than the names: num1 and num2 appear in all three sub programs, but only in maths3 are they parameters.

Which functions are called in the code?

Answer

Only maths2, on the line output_num = maths2().

That is the one and only call in the whole program. maths1 and maths3 are defined but never called, so their bodies never run. Defining a sub program only tells Python that it exists.

maths2 subtracts 5 from 50 and returns 45, that 45 is stored in output_num, and the print on the last line puts it on the screen. The program’s entire output is:

45

maths3 could not be called the same way in any case. It has two parameters, so calling it as maths3() with empty brackets would raise a TypeError: it has to be given two numbers, as in maths3(6, 7).

How do you know which lines of code belong to a function or sub program?

Answer

By the indentation.

A sub program’s body is every line indented underneath its def line, and it ends at the first line that goes back to the left hand margin. In maths1 that is the three lines from num1 = 50 down to the return; the blank line and the def maths2(): line that follow are not part of it.

The last two lines of the program, output_num = maths2() and print(output_num), start at the left hand margin, so they belong to no sub program at all. They are the main program, and they are the only lines that run on their own.

Indentation is not decoration here, it is what decides which lines belong to what. Indent the output_num = maths2() line by four spaces and it stops being the main program and becomes part of maths3 instead. Nothing then calls maths2, output_num is never created, and the print on the last line fails with NameError: name 'output_num' is not defined. Try it.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The sub program name is spelled identically everywhere it is used. Capital letters count.
  • There is a colon at the end of the def line.
  • The body of the sub program is indented, and every line of it by the same amount. Never mix tabs and spaces in one file.
  • The call is not indented inside the sub program.
  • The sub program is defined before it is called.
  • The number of arguments at the call matches the number of parameters on the def line.