Three Kinds of Function
Every function you will ever call is one of three kinds, and the only thing that separates them is where the code lives and therefore what you have to do before you can call it. Once it is called, all three behave identically — a name, some brackets, and the code runs.
len(marks)Already there. Python knows the name from the moment it starts.
math.sqrt(25)Written by somebody else, in a file. You import it first.
area(12, 5)Written by you, in your own program, with def.
1Built-in functions
These come with Python. There is nothing to import and nothing to write — the names simply exist, and you have been using them since your first program:
marks = [72, 65, 88, 91, 54]
print(len(marks))
print(max(marks))
print(min(marks))
print(sum(marks))
print(sorted(marks))
print(abs(-7))
print(round(3.14159, 2))5 91 54 370 [54, 65, 72, 88, 91] 7 3.14
print() itself is one, and so are input(), int(), str(), float(), list(), type() and range(). Python 3.12 ships about seventy of them — you are expected to know a couple of dozen, not all of them.
if, for and def are keywords — part of the grammar of the language. len is a name, like any other, that happens to have a function stored in it before your program starts. That is why you can accidentally destroy one by writing len = 5, and why you should not.2Functions defined in a module
A module is a file of ready-made code. Python ships with hundreds, and the functions inside them are not available until you import the module they live in:
import math
import statistics
print(math.sqrt(144))
print(math.ceil(4.2))
print(statistics.mean([10, 20, 30]))12.0 5 20
The dot says which module the name came from: math.sqrt is the sqrt that belongs to math. The other import form lifts the name out so no dot is needed:
from math import sqrt, floor
print(sqrt(81))
print(floor(9.8))9.0 9
sqrt(25) on its own raises NameError: name 'sqrt' is not defined, because Python only knows the names it starts with plus the ones you have brought in or written.3User-defined functions
The third kind is the one this chapter is really about: the functions you write yourself, with def, because no built-in and no module does the job you need.
# nobody has written this one for you — it is your problem, so it is your function
def area_of_rectangle(length, breadth):
return length * breadth
print(area_of_rectangle(12, 5))
print(area_of_rectangle(7, 3))60 21
Python has no area_of_rectangle, and asking for one without writing it gives the same error as a forgotten import:
print(area_of_circle(5))Traceback (most recent call last):
File "not_defined.py", line 1, in <module>
print(area_of_circle(5))
^^^^^^^^^^^^^^
NameError: name 'area_of_circle' is not defined4Underneath, they are the same thing
The three kinds are a story about where the code came from, not about what a function is. Ask Python what each one is and it barely distinguishes them:
import math
def mine():
pass
print(type(len))
print(type(math.sqrt))
print(type(mine))<class 'builtin_function_or_method'> <class 'builtin_function_or_method'> <class 'function'>
import for a module function, a def for your own.| Kind | Written by | Before you call it | Example |
|---|---|---|---|
| Built-in | The Python developers | Nothing | len(marks) |
| Module | The Python developers, or anyone | import the module | math.sqrt(25) |
| User-defined | You | def it, above the call | area(12, 5) |
5Recap
len, max, min, sum, print, input, int, str, range. No import, no def.
math.sqrt, statistics.mean, random.randint. The dot names the module the function came from.
def, because no built-in and no module solves your particular problem.
A name and brackets. Only the preparation differs — nothing, an import, or a def.
- 1
List five built-in functions you used in Class 11 and say what each returns.
Hint ·
len,sum,max,sorted,roundare a good five. - 2
Print the square root of 625 twice — once with
import mathand once withfrom math import sqrt.Hint ·
math.sqrt(625)for the first,sqrt(625)for the second. Both give 25.0. - 3
Write a user-defined function that returns the perimeter of a rectangle, and call it twice.
Hint ·
return 2 * (length + breadth). There is no built-in for this, which is exactly why you write one.
Which of these needs an import before you can call it?
What happens if you call area_of_circle(5) without defining it?
What really separates the three kinds of function?