Functions

INPUT · Slides

Hand a value in as you call

01 / 07

It says the same thing every time

The function you made last lesson said the same thing however often you called it.

What you really want is "hello, Yui", "hello, Sora" — something that changes with who you are talking to. That is why a function can take values in.

def greet():    print("hello")greet()greet()

Result

hello
hello

02 / 07

Put a name in the brackets

Write a name inside the () that has been empty until now, and you can hand a value in when you call.

Inside the function, that name holds whatever you handed in. Below, the name is name: call greet("Yui") and name is "Yui" for the length of that call.

def greet(name):    print(f"hello, {name}")greet("Yui")greet("Sora")

Result

hello, Yui
hello, Sora

03 / 07

The value you hand in, the name that catches it

A value handed to a function is called an argument. The two sides have their own words.

  • the name in the def brackets … the name that catches it, called a parameter
  • the "Yui" you write when calling … the value handed in, the argument itself

You do not have to remember the words. The one thing that matters is that the name in the brackets takes whatever you handed in.

def double(number):    print(number * 2)double(5)double(100)

Result

10
200

04 / 07

A variable can be handed in

What you hand in does not have to be written out in full. A variable works, and so does the result of a sum.

What travels is not the name of the variable but the value that was in it. That is why the name inside the function and the name outside can happily differ.

def double(number):    print(number * 2)price = 150double(price)double(price + 50)

Result

300
400

05 / 07

Put it together with a loop

Walk a list and hand in one item at a time, and the same piece of work covers everybody.

The body of the function is written once, yet three people got greeted. Note again that the loop variable is person and the catching name is name. Only the value travels.

def greet(name):    print(f"hello, {name}")names = ["Yui", "Sora", "Haru"]for person in names:    greet(person)

Result

hello, Yui
hello, Sora
hello, Haru

06 / 07

Forget to hand one in and it errors

One catching name means exactly one value every time you call. Forget it and a TypeError stops you.

The message is long, but it says everything. missing 1 required positional argument means "one required argument is missing", and the name at the end is the one you left out.

def greet(name):    print(f"hello, {name}")greet()

Result

TypeError: greet() missing 1 required positional argument: 'name'

07 / 07

Too many is an error too

Hand in too many and it fails the other way. takes 1 positional argument but 2 were given means "it only takes one and two arrived".

The number caught and the number handed in must match exactly. That is the basic rule of arguments.

You can now hand a value *into* a function. Next it is the other way round — getting a value back out.

def greet(name):    print(f"hello, {name}")greet("Yui", "Sora")

Result

TypeError: greet() takes 1 positional argument but 2 were given