LambdaLabTM
Computer Science · Class 12 · Functions
FunctionsTypes⏱️ 13 min read

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.

Built-in
len(marks)

Already there. Python knows the name from the moment it starts.

From a module
math.sqrt(25)

Written by somebody else, in a file. You import it first.

User-defined
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:

builtins.py
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))
Output
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.

Tip
They are not keywords. 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:

module_functions.py
import math
import statistics

print(math.sqrt(144))
print(math.ceil(4.2))
print(statistics.mean([10, 20, 30]))
Output
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_import.py
from math import sqrt, floor

print(sqrt(81))
print(floor(9.8))
Output
9.0
9
Watch Out
Forget the import and the name does not exist. 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.

user_defined.py
# 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))
Output
60
21

Python has no area_of_rectangle, and asking for one without writing it gives the same error as a forgotten import:

not_defined.py
print(area_of_circle(5))
Output
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 defined

4Underneath, 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:

type_of_each.py
import math

def mine():
    pass

print(type(len))
print(type(math.sqrt))
print(type(mine))
Output
<class 'builtin_function_or_method'>
<class 'builtin_function_or_method'>
<class 'function'>
Key Takeaway
Calling one is identical in all three cases: a name, brackets, and whatever the function needs inside them. What differs is only what you did beforehand — nothing for a built-in, an import for a module function, a def for your own.
KindWritten byBefore you call itExample
Built-inThe Python developersNothinglen(marks)
ModuleThe Python developers, or anyoneimport the modulemath.sqrt(25)
User-definedYoudef it, above the callarea(12, 5)
three_kinds.py

5Recap

Built-in: already there

len, max, min, sum, print, input, int, str, range. No import, no def.

Module: import it first

math.sqrt, statistics.mean, random.randint. The dot names the module the function came from.

User-defined: you write it

def, because no built-in and no module solves your particular problem.

Calling is the same for all three

A name and brackets. Only the preparation differs — nothing, an import, or a def.

✍️ Now write these yourself
  1. 1

    List five built-in functions you used in Class 11 and say what each returns.

    Hint · len, sum, max, sorted, round are a good five.

  2. 2

    Print the square root of 625 twice — once with import math and once with from math import sqrt.

    Hint · math.sqrt(625) for the first, sqrt(625) for the second. Both give 25.0.

  3. 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.

Quick Check

Which of these needs an import before you can call it?

Quick Check

What happens if you call area_of_circle(5) without defining it?

Quick Check

What really separates the three kinds of function?