Back to Study Material Python

Functions

A function is a named, reusable block of code defined with the def keyword. Syntax: def function_name(parameters): followed by an indented body. Call it by...

Functions

Defining and Calling Functions

A function is a named, reusable block of code defined with the def keyword. Syntax: def function_name(parameters): followed by an indented body. Call it by writing function_name(arguments). Example: def greet(name): return "Hello, " + name; then print(greet("Alice")) outputs "Hello, Alice". Functions make code modular, easier to test, and allow logic to be reused without copy-pasting. Every function returns a value; if no return statement is present, it returns None implicitly.

Parameters vs Arguments

Parameters are the variable names listed in the function definition; arguments are the actual values passed when the function is called. def add(a, b): return a + b — here a and b are parameters. add(3, 5) — here 3 and 5 are arguments. Python supports four kinds of parameters: positional, default, keyword, and variable-length (*args and **kwargs). Understanding these types is crucial for writing flexible, reusable functions.

Return Values

The return statement exits the function and optionally sends a value back to the caller. A function can return any Python object including lists, tuples, and even other functions. To return multiple values, use a tuple: return x, y is equivalent to return (x, y). The caller unpacks this: a, b = swap(1, 2). Once return executes, no further code in the function runs. A bare return (with no expression) returns None, same as falling off the end of the function.

Default Parameter Values

You can assign default values to parameters: def greet(name, greeting="Hello"): return f"{greeting}, {name}!". Calling greet("Bob") uses the default greeting; greet("Bob", "Hi") overrides it. Important rule: parameters with defaults must come after parameters without defaults. A classic gotcha: never use a mutable object as a default — def append_item(val, lst=[]): lst.append(val); return lst. The list is created once and reused across calls, causing unexpected behaviour. Use None as the default and create the list inside the function body.

Scope Inside Functions

Variables assigned inside a function are local to that function. They do not exist outside it, and they shadow any global variable with the same name. To read a global inside a function, just use the name — Python looks it up via the LEGB rule. To assign to a global variable inside a function, you must declare it: global counter; counter += 1. Without the global declaration, the assignment creates a new local variable, leaving the global unchanged.

Exam Tip

Common exam question: what does a function that has no return statement return? It returns None. Another: what is the difference between print() and return inside a function? print() displays output to the terminal but does not send a value to the caller. return sends a value that the caller can use in further expressions. A function that only prints cannot be used in an expression like result = my_func() + 5 because it returns None, causing a TypeError.