Master Python

First steps

10 / 253
Contents

Contents

Master Python

0 of 253 complete

7 min read

Writing your first function

A small machine with a name: something goes in, something comes back.

Everything so far has been a program that says something out loud. The problems at the end of each section ask for something slightly different: a small machine that takes something in and hands something back.

A vending machine is the picture to hold on to. You put a coin in and it hands you a drink. It does not announce the drink to the room and keep it; it gives it to you, and what you do with it next is your business.

In Python a small machine like that is called a function. It has a name, it can be handed one or more pieces of information, and it can hand one thing back. The whole idea fits in three lines.

def double(number):
    return number * 2

print(double(5))
A function called double, and one use of it.

Five things there are worth naming.

  • `def` is short for define, and it starts the description of a new function. It does not carry anything out; it tells Python what this function will be, for later.
  • `double` is the name you chose. You use it afterwards to ask for the function by name.
  • `number` is a parameter: a stand-in name for whatever gets handed in. Inside the function, number means whichever value was handed over that time.
  • The colon and the indent. The colon at the end of the first line says the body is coming, and the body is indented by four spaces. Those spaces are how Python knows which lines are inside the function and which are not.
  • `return` hands a value back to whoever asked for it. The function stops at that point.

The last line, print(double(5)), does two things in one go, and you read it from the inside out. double(5) runs the function with 5 handed in, and the function gives back 10. Then print writes that 10 out. Asking for a function by name like this is called calling it, and the value you hand over is an argument.

Try CodePython

Change return number * 2 to return number * 3 and run it again. Both lines of output change, because both go through the same function.

Functions are not only for numbers. A parameter can stand for a piece of text just as easily, and the body can build a new piece of text out of it.

Try CodePython

Change "Ada" to your own name and run it again. Then rewrite the return line as an f-string, return f"Hello, {name}.", and check that the output is identical.

A function can take more than one parameter. Name them in the brackets, separated by commas, and hand over the same number of values in the same order when you call it.

Try CodePython

Swap the two values on the last line so the number comes first, and run it again to see what a sentence built from the wrong parameters looks like.

Every function so far has had a body of exactly one line. It does not have to. Here is one that says what it is about to do, and then hands an answer back.

Try CodePython

Delete the print line and run it again. The answer that comes back is the same: only the return line decides what the function hands over.

The body can be as many lines as you need, as long as they are all indented by the same four spaces and one of them is a return. Until the next section each line has to do its work in one go, because you have not yet met a way to give a half-finished answer a name.

All lessons in Master Python