Study Guide: Functions
Module 9 Review Materials
Key Terms
Function: A named, reusable block of code that performs a specific task. Defined with the def keyword.
Parameter: A variable listed in a function's definition that acts as a placeholder for input data.
Argument: The actual value passed to a function when it is called.
Return Value: The value a function sends back to the caller using the return statement.
None: Python's special value representing "no value." Returned by functions without a return statement.
Docstring: A documentation string placed as the first line of a function body, enclosed in triple quotes.
Default Parameter: A parameter with a pre-assigned value that is used when no argument is provided for it.
Keyword Argument: An argument passed using name=value syntax, allowing arguments to be given in any order.
*args: Syntax for collecting extra positional arguments into a tuple.
**kwargs: Syntax for collecting extra keyword arguments into a dictionary.
Scope: The region of a program where a variable is accessible. Python uses Local, Enclosing, Global, and Built-in scopes (LEGB).
Local Variable: A variable defined inside a function; only accessible within that function.
Global Variable: A variable defined outside all functions; accessible throughout the module.
Lesson 1: Defining Functions with def
Key Concepts
- Use def function_name(): to define a function
- The function body must be indented
- Defining a function does not run it — you must call it with parentheses
- Include a docstring to document what the function does
- Name functions with descriptive verbs in snake_case
Lesson 2: Parameters and Return Values
Key Concepts
- Parameters are defined in the function header; arguments are passed when calling
- Positional arguments must match parameters by order
- return sends a value back and immediately ends the function
- print() displays output but does not give back a usable value
- Functions can return multiple values as a tuple
- Functions without return return None
Lesson 3: Default and Keyword Arguments
Key Concepts
- Default values make parameters optional: def f(x, y=10):
- Parameters with defaults must come after those without
- Keyword arguments use name=value at the call site
- Positional arguments must come before keyword arguments in a call
- *args collects variable positional arguments into a tuple
- **kwargs collects variable keyword arguments into a dictionary
- Parameter order: regular, defaults, *args, **kwargs
Lesson 4: Scope – Local vs Global
Key Concepts
- Variables defined inside a function are local — they exist only during that function call
- Variables defined outside functions are global — functions can read them but not modify them
- Assigning to a variable inside a function creates a new local variable (does not change global)
- The global keyword allows modifying a global variable from inside a function
- LEGB rule: Python resolves names by searching Local, Enclosing, Global, then Built-in scopes
- Best practice: avoid global; use parameters and return values instead
Common Mistakes to Avoid
- Forgetting parentheses when calling a function: greet vs greet()
- Using print instead of return — print displays output but does not give back a value you can use
- Wrong argument count — passing too many or too few arguments raises TypeError
- Putting defaults before required parameters — def f(a=1, b) is a syntax error
- Expecting global changes without the global keyword — assignment inside a function creates a local variable
- Shadowing built-in names — naming a variable list, str, or print overrides the built-in
- Mutable default arguments — using def f(items=[]) can cause unexpected behavior; use None as default instead
Review Questions
- What keyword defines a function? What punctuation marks the end of the function header?
- What is the difference between defining and calling a function?
- What is the difference between print() and return?
- What does a function return if it has no return statement?
- How do you return multiple values from a function?
- What is the difference between a parameter and an argument?
- What are default parameter values and why are they useful?
- When would you use *args? When would you use **kwargs?
- What is the LEGB rule? List the four scopes in order.
- Why should you avoid using the global keyword? What is a better alternative?