You have used functions made by others: print(), len(), range(). Now you write your own. A function is a group of lines with a name, so that you write them once and use them as often as you like. In this course you define and call a function, give it values to work with, get an answer back with return, set default values, make functions that answer yes or no, and let one function use another. You learn where a variable lives and why a variable made inside a function is not known outside it, and how to test a function with a table of cases. A toolbox of functions is the project at the end. The practice runs your functions on many inputs and tells you which ones fail.
6 lessons, about 2 h 55 min6 simulations14 problems12 practice questionsPython in every lesson
A function is a group of lines with a name. The word def starts it, then comes the name, brackets and a colon, and the indented lines are its body. Defining a function only stores it, and calling it with its name and brackets runs the lines. A function can be called as often as you like, so you write the lines once. Functions avoid repeating, make a fix easy to do in one place, and make a program clear to read. A function that is defined but never called does nothing.
Most functions take values in and give an answer back. The names in the def line are parameters, and the values in the call are arguments. return sends a value back to the place that called the function, and ends the function at once. print only shows something on the screen, and the program cannot use it. A function without a return gives back None. A call must give a value for every parameter, or Python shows a TypeError.
A parameter can have a default value, which Python uses when the call gives no argument for it. Arguments can be named, so the order is free and the call reads clearly. A function can return True or False to answer a question, and a name that starts with is_ tells the reader that it does. One function can call another, so small functions that do one job can be put together like building blocks. A function that returns a value should return it in every path.
A variable made inside a function is local: it lives only while the function runs, and Python does not know its name outside, so using it there gives a NameError. To get a result out, return it. A function can read a variable from outside, but if it assigns to the same name, it makes a new local variable and the outside one stays. Parameters are local variables too. Rely on parameters and return, and not on outside variables, so that a function works alone and is easy to test.
A function is only useful if it is right, so test it. A test is a table of cases: an input, and the answer that you expect from the rules, not from the function. Include the edges, such as 49, 50 and 51 for a grade, the smallest and the biggest allowed input, and a case with an unusual value. A loop over the table prints PASS or FAIL for each case. A short note in triple quotes, the docstring, says what the function does and can be read back with help. When a test fails, follow the function by hand for that input.
In this project we build a toolbox of small functions that each do one job: the area of a rectangle and of a circle, a temperature conversion, a leap year test and a letter grade. Each function takes its values as parameters and gives its answer with return, has a docstring, and is tested with a table of cases. The main program is then a few short lines that use the tools. We plan the tools first, build and test them one by one, and extend the toolbox with tools of our own.
Log in to join the discussion.
Loading…