Functions and Modular Design
Summary
This chapter introduces functions as the primary tool for organizing and reusing code. Students will learn to define functions with parameters and return values, understand variable scope (local vs global), use default and keyword arguments, and write documentation with docstrings. The chapter emphasizes modular design principles that lead to well-structured, maintainable programs.
Concepts Covered
This chapter covers the following 17 concepts from the learning graph:
- Functions
- Function Definition
- Function Call
- Parameters
- Arguments
- Return Statement
- Return Values
- None Type
- Default Parameters
- Keyword Arguments
- Positional Arguments
- Multiple Return Values
- Variable Scope
- Local Variables
- Global Variables
- Global Keyword
- Docstrings
Prerequisites
This chapter builds on concepts from:
- Chapter 1: Introduction to Computer Science
- Chapter 2: Python Fundamentals
- Chapter 4: Control Flow
- Chapter 5: Working with Strings
Monty says: Let's code this!
Welcome back, coders! This chapter is a huge milestone. You're about to learn about functions — the single most important tool for writing clean, organized, reusable code. Once you master functions, you'll never look at programming the same way again. Let's do this!
What Are Functions?
Imagine you're baking cookies. Every time you want cookies, you don't reinvent the recipe from scratch. You follow the same set of steps: preheat the oven, mix the ingredients, scoop the dough, bake for 12 minutes. That recipe is a set of reusable instructions you can follow again and again.
A function in Python works the same way. It's a named block of code that performs a specific task. You write it once, and then you can use it as many times as you want — no copy-pasting needed. Functions are the recipe cards of programming.
You've actually been using functions since Chapter 2 without realizing it. Every time you wrote print("Hello!") or len("Python") or input("What's your name? "), you were calling a function that someone else already wrote for you. Now it's time to write your own.
Why do functions matter? Here are the big reasons:
- Reusability — Write code once, use it everywhere
- Organization — Break big programs into smaller, manageable pieces
- Readability — Give chunks of code meaningful names so others (and future you) can understand them
- Debugging — When something breaks, you only need to fix it in one place
- Collaboration — Different team members can work on different functions
In short, functions turn spaghetti code into a well-organized recipe book.
Diagram: Function Anatomy
Function Anatomy Interactive Diagram
Type: diagram
sim-id: function-anatomy
Library: p5.js
Status: Specified
Bloom Level: Remember (L1) Bloom Verb: identify, label
Learning Objective: Students will be able to identify the parts of a Python function definition — the def keyword, function name, parameters, colon, body, and return statement — and describe the purpose of each.
Purpose: An interactive labeled diagram of a Python function that highlights each part when the student hovers over it, showing the name and purpose of each syntactic element.
Layout: - Center of canvas: A large, syntax-highlighted Python function displayed as styled text:
1 2 3 4 | |
Hover zones and descriptions:
defkeyword — "The def keyword tells Python you're defining a new function."greet(function name) — "The function's name. You'll use this name to call the function later."(name)(parameter) — "Parameters are placeholders for data the function will receive. They go inside parentheses.":(colon) — "The colon marks the beginning of the function body. Everything indented below is part of the function.""""Say hello to someone."""(docstring) — "A docstring describes what the function does. It's the first line inside the function, wrapped in triple quotes."message = "Hello, " + name + "!"(body) — "The body is where the function does its work. All body lines must be indented."return message(return statement) — "The return statement sends a value back to whoever called the function."
Interactive elements: - Hover over any part to highlight it with a colored background and show its description in the tooltip panel - Click any part to "lock" the tooltip so it stays visible while you read it - A "Show All Labels" button draws arrows from each part to a labeled list
Color scheme:
- def keyword: blue
- Function name: green
- Parameters: orange
- Colon: gray
- Docstring: purple
- Body: default/white
- Return statement: red
Visual style: Monospaced font for code, clean modern tooltip panel Responsive: Font size scales with canvas width; tooltip repositions on smaller screens
Instructional Rationale: Hover-to-reveal supports the Remember level by letting students explore each syntactic element at their own pace. The visual mapping between syntax and purpose builds a mental model of function structure before students write their own.
Defining Your First Function
Let's write our first function. A function definition is the code that creates a new function. It starts with the def keyword, followed by the function's name, parentheses, and a colon. The function's code goes on the next lines, indented by four spaces.
1 2 | |
That's it! You just defined a function called say_hello. But here's the important part: defining a function doesn't run it. It's like writing a recipe in your recipe book — the cookies don't magically appear until you actually follow the recipe.
To run the function, you need to make a function call:
1 | |
When Python sees this line, it jumps up to where you defined say_hello, runs the code inside it, then comes back to where it left off. You can call the same function as many times as you want:
1 2 3 | |
Here's the basic pattern for defining and calling a function:
| Step | What You Write | What It Does |
|---|---|---|
| 1. Define | def function_name(): |
Creates the function (stores the recipe) |
| 2. Write body | Indented code below def |
Describes what the function does |
| 3. Call | function_name() |
Runs the function (follows the recipe) |
Naming Your Functions
Function names follow the same rules as variable names:
- Use lowercase letters and underscores (snake_case):
calculate_area,get_user_input - Start with a letter or underscore, not a number
- Make names descriptive —
calculate_taxis better thanct - Use verbs since functions do things:
greet_user,find_maximum,print_report
Parameters and Arguments
A function that does the exact same thing every time is useful, but limited. What if you want to customize what it does? That's where parameters come in.
Parameters are variables listed inside the parentheses of a function definition. They act as placeholders for data that will be provided when the function is called. Think of them like blank lines on a form — the function says "I need a name here," and you fill it in when you use the function.
1 2 | |
In this example, name is a parameter. When you call the function, you provide a value for that parameter — and that value is called an argument:
1 2 3 | |
Output:
1 2 3 | |
Here's a quick way to remember the difference:
| Term | Where It Appears | What It Is |
|---|---|---|
| Parameter | In the function definition | The placeholder (the blank line on the form) |
| Argument | In the function call | The actual value you fill in |
You can have multiple parameters — just separate them with commas:
1 2 3 4 5 6 | |
Monty says: You've got this!
Think of a function like a vending machine. The parameters are the slots where you insert your selections (like choosing a snack and a drink). The arguments are the actual buttons you press. The function's body is all the machinery inside, and the output is the snack that drops out. Same machine, different snacks depending on what you press!
Return Values: Getting Results Back
So far, our functions have printed things. But what if you want a function to calculate a result and give it back to you so you can use it later? That's what the return statement does.
The return keyword sends a value — called a return value — back to the code that called the function. It's like asking a friend to go check the temperature outside. You don't want them to just shout the temperature into the void — you want them to come back and tell you so you can decide whether to wear a jacket.
1 2 3 4 5 | |
Notice the difference from before: instead of print inside the function, we use return. The function sends the value back, and we can store it in a variable, use it in another calculation, or do whatever we want with it.
1 2 3 4 5 6 7 | |
Here are some key facts about return:
- A function can have multiple
returnstatements (often insideif/elseblocks), but only one will execute - Once
returnruns, the function immediately stops — any code after thereturnline won't execute - A function without a
returnstatement automatically returns a special value calledNone
The None Type
What happens when a function doesn't explicitly return anything? Python gives it a return value of None. None is a special Python value — it represents "nothing" or "no value." Its type is the None type (technically NoneType).
1 2 3 4 5 6 | |
None isn't the same as zero, an empty string, or False. It's its own thing — it means "this variable doesn't hold any meaningful value." You'll see None come up often when debugging, so it's good to know what it means.
Diagram: Function Call Flow
Function Call Flow MicroSim
Type: microsim
sim-id: function-call-flow
Library: p5.js
Status: Specified
Bloom Level: Understand (L2) Bloom Verb: trace, explain
Learning Objective: Students will be able to trace the flow of execution when a function is called, including how arguments are passed, how the function body runs, and how the return value flows back to the caller.
Purpose: An animated step-through showing a function call, visualizing the jump from caller code to the function definition and back, with arguments flowing in and the return value flowing out.
Canvas layout: - Left side: "Caller Code" panel showing:
1 2 | |
1 2 | |
Animation steps:
- Highlight
add(3, 5)in the caller — Narration: "Python sees a function call: add(3, 5)" - Animate "3" bubble flowing from caller to parameter
a— Narration: "Argument 3 is assigned to parameter a" - Animate "5" bubble flowing from caller to parameter
b— Narration: "Argument 5 is assigned to parameter b" - Highlight
return a + bin the function — Narration: "Python evaluates a + b = 3 + 5 = 8" - Animate "8" bubble flowing back from function to
x =— Narration: "The return value 8 is sent back and stored in x" - Highlight
print(x)— Narration: "Python prints x, which is 8"
Interactive controls: - "Step" button: Advance one step at a time - "Auto Play" button: Animate all steps with a 1.5-second delay - "Reset" button: Return to starting state - Speed slider to adjust animation pace
Visual style: - Code panels use monospaced font with syntax highlighting - Value "bubbles" are colored circles with the value displayed inside - Active lines have a yellow highlight - Arrows animate smoothly with easing
Responsive: Panels stack vertically on narrow screens
Instructional Rationale: Step-through animation supports the Understand level by making the invisible execution flow visible. Students often struggle with the concept of "jumping" to a function and back — this animation makes the jump explicit. Value bubbles show how arguments become parameters and how return values flow back.
Positional and Keyword Arguments
When you call a function with multiple parameters, there are two ways to pass the arguments.
Positional arguments are the default. The first argument goes to the first parameter, the second argument goes to the second parameter, and so on. Order matters!
1 2 3 4 5 | |
See what happened in the second call? We swapped the order, so the arguments got matched to the wrong parameters. With positional arguments, you have to remember which order to use.
Keyword arguments let you specify exactly which parameter gets which value by using the parameter's name:
1 2 | |
With keyword arguments, order doesn't matter — Python matches each argument to the right parameter by name. This makes your code clearer, especially when a function has many parameters.
You can even mix positional and keyword arguments, but there's one rule: positional arguments must come first.
1 2 | |
| Argument Type | How It Works | Order Matters? | Example |
|---|---|---|---|
| Positional | Matched by position (1st, 2nd, 3rd...) | Yes | greet("Alice", 16) |
| Keyword | Matched by parameter name | No | greet(name="Alice", age=16) |
| Mixed | Positional first, then keyword | Partially | greet("Alice", age=16) |
Monty says: Watch out!
A common mistake is putting keyword arguments before positional ones. Python will throw a SyntaxError if you try this. Always put positional arguments first, keyword arguments second!
Default Parameters
Sometimes you want a parameter to have a fallback value in case the caller doesn't provide one. That's what default parameters do. You set a default value in the function definition using =:
1 2 3 4 5 | |
The parameter greeting has a default value of "Hello". If you call greet with only one argument, greeting automatically becomes "Hello". If you provide a second argument, it replaces the default.
Default parameters are incredibly handy:
1 2 3 4 5 6 7 | |
There's one important rule: parameters with default values must come after parameters without defaults. Otherwise Python gets confused about which argument matches which parameter.
1 2 3 4 5 | |
Multiple Return Values
Here's a Python superpower: a single function can return multiple return values at once. You do this by separating the values with commas after return. Under the hood, Python packs them into a tuple (which you'll learn more about in Chapter 10).
1 2 3 4 5 6 | |
This is really useful when a function needs to compute several related pieces of information:
1 2 3 4 5 6 7 8 9 10 11 | |
The left side of the assignment uses unpacking — each variable on the left matches up with one returned value on the right. If you use the wrong number of variables, Python will raise an error.
Diagram: Arguments vs Parameters
Arguments vs Parameters Interactive Comparison
Type: diagram
sim-id: args-vs-params
Library: p5.js
Status: Specified
Bloom Level: Understand (L2) Bloom Verb: differentiate, classify
Learning Objective: Students will be able to distinguish between parameters (in the definition) and arguments (in the call), and between positional, keyword, and default argument types.
Purpose: An interactive side-by-side display that shows a function definition and multiple function calls, with color-coded connections between arguments and their corresponding parameters.
Canvas layout: - Top half: Function definition displayed prominently:
1 2 | |
name (blue), age (green), city (orange)
- Bottom half: Three function call examples the student can switch between using tabs:
- Tab 1 "Positional": make_profile("Alice", 16, "Austin")
- Tab 2 "Keyword": make_profile(age=16, name="Alice", city="Austin")
- Tab 3 "Default Used": make_profile("Alice", 16)
Interactive elements:
- Click a tab to show a different function call style
- Animated lines connect each argument to its matched parameter, using the parameter's color
- Hover over any argument to highlight both the argument and the parameter it maps to
- For Tab 3, the default value for city glows to show it's being used automatically
- Below each call: text showing the return value output
Visual style: - Monospaced font for code - Smooth curved connecting lines between arguments and parameters - Subtle animation when switching tabs
Color scheme:
- name parameter/argument: blue
- age parameter/argument: green
- city parameter/argument: orange
- Default value indicator: dashed orange border
Responsive: Layout stacks vertically on narrow screens
Instructional Rationale: Color-coded matching lines make the abstract mapping between arguments and parameters concrete and visible. Showing three different calling styles in tabs lets students compare positional, keyword, and default behaviors side by side. This supports the Understand level by helping students differentiate between these closely related concepts.
Variable Scope: Who Can See What?
Here's a concept that trips up a lot of new programmers, so pay close attention. Variable scope determines where in your code a variable can be accessed. Not every variable is available everywhere.
Local Variables
A local variable is a variable created inside a function. It only exists while the function is running, and it can only be used inside that function. Once the function finishes, the local variable vanishes like a Snapchat message.
1 2 3 4 5 6 | |
The variable area was born inside calculate_area and dies when the function ends. The rest of your program has no idea it ever existed.
Parameters are also local variables. In the example above, length and width only exist inside calculate_area.
Global Variables
A global variable is a variable created outside of any function, in the main body of your program. It can be read from anywhere — inside functions, outside functions, everywhere.
1 2 3 4 5 6 | |
But here's the catch: you can read a global variable from inside a function, but you cannot change it without special permission. If you try to assign a new value to it inside a function, Python creates a new local variable with the same name instead:
1 2 3 4 5 6 7 | |
This error happens because Python sees count = on the left side and assumes you're trying to create a local variable called count. But you're also trying to use count on the right side before it's been created locally. Confusing? It's one of Python's quirks.
The Global Keyword
If you truly need to modify a global variable from inside a function, you must use the global keyword to tell Python "I mean the global one, not a new local one":
1 2 3 4 5 6 7 8 9 10 | |
However, using global is generally considered a bad habit. It makes your code harder to understand and debug because any function could change any variable at any time. It's like giving everyone in school the keys to the janitor's closet — technically possible, but things get messy fast.
Best practice: Instead of using global variables, pass values into functions as parameters and get results back using return. This keeps your functions self-contained and predictable.
Here's a summary of scope rules:
| Variable Type | Created Where? | Accessible Where? | Lifespan |
|---|---|---|---|
| Local | Inside a function | Only inside that function | While the function runs |
| Global | Outside all functions | Readable everywhere; writable only with global keyword |
Entire program |
| Parameter | In the function definition | Only inside that function (acts like a local variable) | While the function runs |
Monty says: Let's debug this together!
Think of scope like rooms in a house. A local variable is like a note pinned to the fridge in the kitchen — only people in the kitchen can see it. A global variable is like a message on the mailbox outside — everyone can read it. The global keyword is like giving someone the key to the mailbox so they can change the message.
Diagram: Variable Scope Visualizer
Variable Scope Visualizer MicroSim
Type: microsim
sim-id: variable-scope-visualizer
Library: p5.js
Status: Specified
Bloom Level: Apply (L3) Bloom Verb: demonstrate, predict
Learning Objective: Students will be able to predict the value of variables at different points in a program by understanding the rules of local and global variable scope.
Purpose: An interactive code execution visualizer that shows which variables exist (and their values) at each step of a program containing both global variables and function calls with local variables.
Canvas layout: - Left panel: A short Python program displayed with line numbers:
1 2 3 4 5 6 7 8 9 | |
Animation steps:
- Line 1 executes — Global scope shows:
x = 10 - Line 2 executes — Global scope shows:
x = 10, y = 20 - Line 8 calls
double(x)— Local scope box appears:n = 10 - Line 5 executes — Local scope shows:
n = 10, result = 20 - Line 6 returns — Local scope box disappears (variables gone!), Global scope adds:
z = 20 - Line 9 prints — Output shown:
10 20 20
Interactive controls:
- "Step Forward" and "Step Back" buttons
- "Auto Play" button with speed slider
- "Reset" button
- Dropdown to choose from 3 different code examples:
- Example 1: Basic scope (shown above)
- Example 2: Name collision (global and local variable with the same name)
- Example 3: Using global keyword
Visual elements: - Current executing line highlighted in yellow - Global scope box always visible - Local scope box fades in when function is entered, fades out when function exits - Variables appear with a brief "pop" animation when created - Variables fade out and "disappear" when their scope ends
Color scheme: - Global scope border: blue - Local scope border: orange - Current line highlight: yellow - New variable animation: green flash
Responsive: Panels stack vertically on narrow screens
Instructional Rationale: Step-by-step execution with a visible scope display directly addresses the Apply level by requiring students to predict outcomes before stepping forward. The appearing/disappearing local scope box vividly demonstrates that local variables have a limited lifespan. Multiple code examples let students explore edge cases like name collisions and the global keyword.
Docstrings: Documenting Your Functions
As your programs grow, you'll write dozens (or hundreds) of functions. A month from now, will you remember what each one does? What about your teammates? That's why good programmers document their functions with docstrings.
A docstring (short for "documentation string") is a string that appears as the very first line inside a function. It describes what the function does, what parameters it expects, and what it returns. Docstrings are wrapped in triple quotes ("""...""").
1 2 3 4 5 6 7 8 9 10 11 | |
Docstrings aren't just comments — Python actually stores them and makes them available through the built-in help() function:
1 | |
Output:
1 2 3 4 5 6 7 8 9 10 11 | |
Here are some docstring best practices:
- First line: A brief, one-sentence summary of what the function does
- Parameters section: List each parameter and what it represents
- Returns section: Describe what the function returns
- Keep it honest: If the function changes, update the docstring too!
Even a simple one-liner helps:
1 2 3 | |
Monty says: Let's debug this together!
Here's a tip from the pros: write your docstring before you write the function body. It forces you to think about what the function should do before you get lost in the details of how it does it. Future you will thank present you!
Modular Design: Putting It All Together
Now that you know how to write functions, let's talk about when and why to use them. The philosophy of breaking a program into small, focused functions is called modular design.
Imagine you're building a calculator app. You could write the entire thing as one giant block of code — but that would be messy, hard to read, and a nightmare to debug. Instead, you break it into modules (functions), each handling one task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Each function does one thing and does it well. If the add function has a bug, you know exactly where to look. If you want to add multiplication, you just write a new function — you don't have to touch the existing code.
Here are the principles of good modular design:
- Single responsibility: Each function should do one thing
- Descriptive names: A function's name should tell you what it does
- Short and sweet: If a function is longer than 20-30 lines, consider breaking it up
- Documented: Every function should have a docstring
- Self-contained: Functions should rely on their parameters, not global variables
Diagram: Modular Design Builder
Modular Design Builder MicroSim
Type: microsim
sim-id: modular-design-builder
Library: p5.js
Status: Specified
Bloom Level: Analyze (L4) Bloom Verb: organize, deconstruct
Learning Objective: Students will be able to break a monolithic program into modular functions by identifying logical units of work and creating appropriate function boundaries.
Purpose: An interactive exercise where students are given a block of sequential code and must drag-and-drop code lines into function groups, naming each function and connecting them with a call graph.
Canvas layout: - Left panel: "Original Code" — a 12-line sequential program displayed as movable code blocks:
1 2 3 4 5 6 7 8 9 10 11 12 | |
get_info(), check_driving(), show_farewell()
- A "Check My Answer" button that evaluates the student's grouping
Interactive elements: - Drag code lines from the left panel into the function boxes on the right - Each function box shows a header where the student types a function name (pre-populated with suggestions) - Lines snap into place inside a function box - A "Show Solution" button reveals one good way to modularize the code - "Check My Answer" provides feedback: "Lines 1-2 make sense in get_info()" etc. - A "Run Both" button that shows both versions produce the same output
Feedback: - Green check for correctly grouped lines - Orange suggestion for lines that could reasonably go in multiple functions - Red X for clearly misplaced lines (e.g., putting the farewell message in get_info)
Visual style: Code blocks are rounded rectangles with monospaced text; function containers have colored borders (blue, green, orange) Responsive: Panels stack vertically on small screens
Instructional Rationale: Drag-and-drop grouping supports the Analyze level by requiring students to examine relationships between code lines and decide which belong together based on their purpose. This active reorganization task builds intuition for modular design principles that passive reading cannot achieve.
Common Patterns and Best Practices
Let's look at some patterns you'll see again and again in well-written Python functions.
Pattern 1: Input Validation
Functions can check that their arguments make sense before doing any work:
1 2 3 4 5 | |
Pattern 2: Multiple Return Paths
Use if/else to return different values depending on conditions:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Pattern 3: Building Results
Use a local variable to accumulate a result, then return it:
1 2 3 4 5 6 7 8 9 | |
Pattern 4: Functions Calling Functions
Functions can call other functions — this is where modular design really shines:
1 2 3 4 5 6 7 | |
Diagram: Function Pattern Gallery
Function Pattern Gallery Interactive Reference
Type: infographic
sim-id: function-pattern-gallery
Library: p5.js
Status: Specified
Bloom Level: Apply (L3) Bloom Verb: implement, use
Learning Objective: Students will be able to recognize and apply common function patterns (input validation, multiple returns, result accumulation, function composition) when writing their own programs.
Purpose: A clickable gallery of four common function design patterns, each showing a code example with annotations explaining the pattern and when to use it.
Canvas layout: - Grid of 4 cards (2x2), each representing a pattern: - Card 1: "Input Validation" — check before compute - Card 2: "Multiple Return Paths" — different results for different conditions - Card 3: "Result Accumulation" — build up a value in a loop - Card 4: "Function Composition" — functions calling functions
Each card shows: - Pattern name (bold header) - A 4-6 line code example with syntax highlighting - A one-sentence description of when to use this pattern - An icon representing the pattern (shield for validation, fork for multiple paths, stack for accumulation, chain for composition)
Interactive elements: - Click a card to expand it to full width, showing: - The complete code example - Line-by-line annotations - A "When to Use" section with bullet points - A "Try It" section with a small editable code area - Click again or click another card to collapse/switch
Color scheme: - Validation: blue - Multiple returns: green - Accumulation: orange - Composition: purple
Visual style: Card-based layout with rounded corners and subtle shadows Responsive: Cards stack into a single column on narrow screens
Instructional Rationale: A gallery format supports the Apply level by presenting patterns as reusable templates. Students can browse, compare, and practice each pattern independently. The card-based format mirrors how developers use reference documentation in the real world.
Putting It All Together: A Complete Example
Let's combine everything from this chapter into a complete program. We'll build a simple grade report generator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
Notice how each function has a single job, a descriptive name, and a docstring. The main program at the bottom reads almost like plain English: get the name, get the scores, print the report. That's the beauty of modular design.
Monty says: You've got this!
Look at you, writing clean, modular, well-documented code with functions! You've leveled up from writing simple scripts to designing organized programs. That's a massive step forward. The skills you learned in this chapter — parameters, return values, scope, docstrings — will be the foundation for everything else in this course. Great work, coder!
Key Takeaways
- A function is a reusable block of code that performs a specific task. Define it with
def, call it by name. - Parameters are placeholders in the function definition; arguments are the actual values passed in the call.
- The return statement sends a value back to the caller. Without one, the function returns None.
- Positional arguments are matched by order; keyword arguments are matched by parameter name.
- Default parameters provide fallback values when arguments are omitted.
- A function can return multiple values separated by commas (packed as a tuple).
- Variable scope determines where a variable can be accessed: local variables live inside functions; global variables live outside.
- The global keyword lets a function modify a global variable — but avoid it when possible.
- Docstrings document what a function does, its parameters, and its return value.
- Good modular design means each function does one thing, has a clear name, and is self-contained.
Check Your Understanding: What is the difference between a parameter and an argument?
A parameter is a variable in the function definition — it's the placeholder. An argument is the actual value you pass when you call the function. For example, in def greet(name):, name is the parameter. In greet("Alice"), "Alice" is the argument. Think of the parameter as a blank on a form, and the argument as what you write in that blank.
Check Your Understanding: What does a function return if it has no return statement?
It returns None. None is Python's special value meaning "nothing" or "no value." Its type is NoneType. For example:
1 2 3 4 5 | |
Check Your Understanding: Why is using the global keyword generally discouraged?
Using global lets any function change variables that live outside of it, which makes your program harder to understand and debug. If multiple functions can modify the same global variable, it becomes difficult to track where changes happen. Best practice is to pass values into functions as parameters and use return to send results back. This keeps each function self-contained and predictable.