Lessons ·
Writing your own functions
Group a calculation under a name, reuse it and document it, with Awa's prices.
Transcript
Awa adds tax to her prices in several places in her program, and each time, she rewrites the same calculation. The problem comes on the day the rate changes: she must then find and correct every line, with the risk of forgetting one.
So we should write this calculation only once, and then use it everywhere? Yes, and that is the role of a function. Imagine a machine in a workshop. We put something in at the entrance, it always does the same work, and it gives back a result at the exit. A function is exactly that.
To build this machine, Awa writes the keyword def, which comes from the word define. She then writes the name of the function, with_tax. Between parentheses, she states what goes into the machine, here price, which we call a parameter. She ends with a colon.
Next comes the block of the function, in other words the work of the machine, indented by four spaces. Here, this block is a single line, line 2. The return instruction states the result to give back at the exit.
So at this moment, has Python already calculated the price? No, and this is a point that many students find surprising. For now, the machine has only been built, but nobody has started it. Python simply remembers that it exists.
To start it, we must call it, by writing its name followed by a value between parentheses. The value 10,000 then enters the machine, and the parameter price takes this value.
The machine runs its block: it multiplies 10,000 by 1 point 1925, rounds it, and gives back 11,925 at the exit.
The program then resumes where it stopped, and the print function displays this result. Awa can now call this machine as many times as she wants, and if the rate changes, she corrects only one line.
And if she forgets the return instruction? Let's see what happens with this machine, which is supposed to double a price. The value 100 does go in, and its block, on line 2, does calculate 200.
But nobody asks to give this result back, so it is lost. The exit of the machine stays empty. Python indicates this with a special value, None, which means there is nothing.
So that is the difference between print and return. The print function shows a value on the screen, for us humans. The return instruction gives the value back to the program, which can go on using it. It is a bit like the difference between showing a banknote to someone and handing it to them.
But some of her products may not be taxed in the same way. Does she need a second machine? No, she simply adds a second entrance to the same machine, the parameter vat, for the sales tax. And since the rate is almost always the same, she gives it a default value, by writing the equal sign and 0 point 1925 inside the parentheses.
If she calls the function giving only the price, Python uses the default value by itself, and the result is still 11,925.
But if she writes vat equals zero in the call, this value replaces the default one. There is then no tax at all, and the price stays at 10,000.
A good machine has a name that says what it does. That is why we call this function average, and not processing. A function should also do only one thing, because it is then simpler to understand and to correct.
Just under the def, we can write a sentence between three quotation marks, which explains its role. It is like the user guide stuck on the machine. And this user guide, who is it for? It is for the person who will reread the code in six months, who may well be you, and Python's help function can also display it.
The block of this function adds up the grades with sum, counts how many there are with len, then divides. With the grades 12 and 15, the sum is 27, and 27 divided by 2 gives 13 point 5.
I have sometimes seen the keyword lambda in code, is that something else again? It is a tiny function of a single line, which we write on the spot without giving it a name. To understand what it is for, let's take a list of students with their grades.
The sorted function knows how to rank, but it must know by what: the name or the grade? The lambda answers this question. For each student n, it looks at n of 1, in other words the grade, because we count from zero. And reverse equals True asks to start with the highest.
So Inès comes first with 17. Now it is your turn to write your own functions in the lesson, without watching the video again. If you hesitate, that is normal, because it is precisely this effort that will make you remember.
Voices: Google Chirp 3 HD (Despina, Enceladus).
Photos : One Idea LLC, StockSnap, CC0

