Coding/Learn/python/functions
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnFunctions & Modular Code
28 / 32 0%
Lesson 28 of 32
8 mins read

Functions & Modular Code

PreviousNext Lesson
In Plain English

A function is like a button on a coffee machine: you define the recipe once with def, and whenever someone presses the button (calls the function), the machine brews the coffee without rewriting the recipe every morning.

The Mental Model

"Imagine a recipe for chocolate chip cookies. Instead of rewriting the recipe every time a guest visits, you write it once in your recipe book under "bake_cookies()", and whenever you want cookies, you just call that name."

Deep Dive: How It Works

The def keyword defines a function followed by its name, parentheses (), and a colon :. The instructions inside the function must be indented by 4 spaces.

Definition vs Execution: Writing def my_func(): does NOT run the code. It simply teaches Python the recipe. To execute it, you must call the function by writing my_func().

The DRY Principle (Don't Repeat Yourself): If you find yourself copying and pasting the same 3 lines of code in multiple places, wrap them into a function.

Variable Scope: Variables created inside a function are local to that function and disappear when the function finishes running.

Syntax Blueprint

def function_name():
    # 4 spaces of indentation
    do_task_one()
    do_task_two()

# Call the function:
function_name()

Define with def and colon, indent the body, and execute by calling the name with parentheses ().

PointCore Rules to Remember

Point star
The def Keyword: Define the blueprint: def my_function(): followed by indented instructions.
Point star
Call to Execute: Defining does not run the code. You must call it with parentheses: my_function().
Point star
Local Scope: Variables created inside a function stay private inside that function.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting parentheses when calling: my_function

Why it happens: Writing my_function without () references the function object itself instead of executing it.

How to fix: Always include parentheses to execute: my_function().

Pro Tip: Write docstrings right beneath your def line to provide interactive tooltips: def greet(): """Prints a greeting."""

Live Interactive Example

Hit Run Code to see it live
Defining and Reusing a Function
Python 3.12
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: Each time print_banner() was called, Python jumped to the function definition, ran the 3 print lines, and returned.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Define and call a custom function

  • PointDefine a function named say_hello() that prints "Hello from a function!".
  • PointCall say_hello() on a new line.
1
2
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates