Class 11 · Computer Science · Computer Science
Chapter 7: Functions Notes
The big idea
Functions are named, reusable blocks of code; this chapter covers defining and calling them, arguments, return values, variable scope, and the built-in functions and modules of Python.
Things to remember
- A function is a named group of instructions that runs when called. Functions give modularity: they improve readability, shorten code, allow reuse and let team members work in parallel. A user defined function begins with def, and its header ends with a colon.
- A parameter is the name in the function header; an argument is the value passed in the call. Both refer to the same object at first. Default parameters must be trailing: def f(rate, principal = 1000, time = 5) is valid, but a default before a non-default is not.
- return sends a value back to the caller and ends the function. A function with no return value is a void function and gives None. Several values are returned together as a tuple, so area, perimeter = calcAreaPeri(l, b) unpacks them.
- Flow of execution: the interpreter runs top to bottom, skips a function body at its definition, and jumps into it only when it is called. A function must be defined before the call, otherwise a NameError occurs.
- A variable defined outside functions is global; one defined inside is local and exists only while the function runs. A same-named local hides the global. To change a global inside a function, write global followed by its name.
- Built-in functions need no import: abs(-5.7) is 5.7, divmod(17, 5) is (3, 2), pow(5, 2, 4) is 1, sum([2, 4, 7, 3], 3) is 19, len, max, min, input, print, int and others.
- A module is a .py file of functions, used with import modulename and called as modulename.function(). math has ceil, floor, sqrt, pow, factorial, gcd; random has random, randint, randrange; statistics has mean, median, mode.
- from module import name loads only the chosen function, which is then used without the module name, e.g. from math import sqrt. Composition means using one function's result in another, like sqrt(ceil(624.7)). A docstring in triple quotes describes a module.
Words to know
Tap a card to see what it means
Quick quiz
⭐ 0 / 5What does the built-in call divmod(17, 5) return?
What does math.ceil(-9.7) return?
num = 5 is set outside; function f has global num and then num = 10. After calling f(), print(num) shows:
Assertion (A): Calling helloPython() before its def statement produces a NameError. Reason (R): A function body runs only when called, and the interpreter must already know the function definition at that time. Which is correct?
Which function header with default parameters is valid in Python?
Match the pairs
Match each built-in call with its result.
Try this at home
On paper, design three small helper functions for a school day, such as greet(name) and add_marks(a, b). Write each one's parameters and what it returns, then have a family member check the call order.
All Computer Science notesChapter page and exercisesComputer Science syllabus