1. Chapter at a glance
- Modular programming divides a program into separate independent blocks (functions) with specific functionalities.
- A function is a named group of instructions that accomplish a specific task when invoked.
- User-defined functions are created by the programmer using
def; they may accept parameters and may return value(s).
- Advantages of functions: increased readability, reduced code length, reusability, and easier division of work.
- Arguments are values passed during a function call; parameters receive them in the function header; default values can be assigned to trailing parameters.
- A function returns control and value(s) (or None) using the
return statement; multiple values are returned via a tuple.
- Flow of execution begins at the first statement; a function must be defined before it is called.
- Scope of a variable: global (defined outside any function) or local (defined inside a function); the
global keyword is required to modify a global variable inside a function.
- Python standard library provides built-in functions and modules (e.g.,
math, random, statistics) that can be imported and used directly.
2. Key terms and definitions
- Modular programming: The process of dividing a computer program into separate independent blocks of code or sub-problems with different names and specific functionalities.
- Function: A named group of instructions that accomplish a specific task when invoked.
- User-defined function: A function defined by the programmer to achieve a task as per requirement.
- Argument: A value passed to the function during the function call.
- Parameter: The variable in the function header that receives the corresponding argument.
- Default parameter: A pre-decided value assigned to a parameter when the function call does not supply the corresponding argument.
- Void function: A function that performs calculations and displays result(s) but does not return any value.
- Global variable: A variable defined outside any function or block; accessible anywhere in the program.
- Local variable: A variable defined inside a function or block; accessible only inside that function/block.
- Scope of a variable: The part of the program where a variable is accessible.
- Module: A Python (.py) file containing a collection of function definitions.
- Built-in functions: Ready-made functions already defined in the Python interpreter (e.g.,
input(), print(), int(), len()).
- Standard library: An extensive collection of built-in functions and modules available in Python.
3. Syntax and constructs
python
def function_name([parameter1, parameter2, ...]):
# statements
return value(s) # optional
Example:
python
def cyl(h, r):
area_cyl = 2 * 3.14 * r * h
return area_cyl
python
import modulename
Example:
python
import math
python
from modulename import functionname[, functionname, ...]
Example:
python
from math import sqrt, ceil
4. Algorithms and worked logic
-
Creating and calling a user-defined function:
1. Write the function header beginning with def followed by unique name, optional parameters in parentheses, and colon.
2. Indent the function body; include return if a value must be sent back.
3. Call the function by writing its name followed by parentheses containing arguments (if any).
4. Control transfers to the function, executes its body, returns to the call point.
-
Flow of execution:
1. Interpreter executes statements sequentially from top to bottom.
2. On encountering a function definition, statements inside are skipped until a call.
3. On a function call, control jumps to the function definition, executes it, then returns to the statement after the call.
4. Function definition must appear before any call in the program.
-
Handling global variable modification:
1. Prefix the variable name with the keyword global inside the function.
2. Any change made is permanent and visible outside the function.
-
Returning multiple values:
1. Compute the required values inside the function.
2. Return them as a tuple: return (area, perimeter).
3. On the call side, unpack in the same order: area, perimeter = calcAreaPeri(l, b).
-
Using default parameters:
1. Place default parameters only as trailing parameters in the header.
2. If an argument is omitted in the call, the default value is used.
5. Common errors and exam pitfalls
- Calling a function before its definition produces
NameError: name 'function' is not defined.
- Modifying a global variable inside a function without the
global keyword creates a new local variable; changes are lost outside the function.
- Placing a default parameter before a non-default parameter (e.g.,
def f(a=1, b):) is invalid.
- Forgetting that
return sends control back; statements after return are unreachable.
- Assuming a function always returns a value—void functions return
None.
- Using a local variable outside its function produces
NameError.
- Importing a module multiple times is unnecessary; it is loaded only once.
- Incorrect unpacking when a function returns a tuple leads to
ValueError.