AtomLearnAtomLearn
DashboardGoalsPathAchievementsReviewSign In
Programming FundamentalsNot Started

Functions

Define reusable blocks of code with parameters and return values

0%
Knowledge0%
Learn & Drill
Fluency0%
Drill & Speed
Retention0%
Mastery & Review
Confidence0%
All modes
Practice
Knowledge
Fluency
Retention

Knowledge Debt detected

You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.

Explanation

A function is a reusable block of code that performs a specific task. You define it once and call it many times.

python
def add(a, b):
    return a + b

result = add(3, 5)
print(result)  # 8

The `def` keyword

  • Starts every function definition, followed by a name, parentheses, and colon: def function_name():
  • The indented block below is the function body — everything at that indentation level belongs to the function
python
def greet():
    print("Hello, world!")

Parameters

python
def add(a, b):
    return a + b
  • a and b are — the inputs the function expects

Arguments

  • 3 and 5 are — the actual values passed when you call the function
python
add(3, 5)
  • Parameters are the names in the definition; arguments are the values passed at call time

`return`

  • return sends a value back to the caller, which can be stored or used directly
def add(a, b):
    return a + b

result = add(2, 3)
print(result)
print(add(10, 5))
What will this print?

No `return` → `None`

  • Functions without a return statement return None by default
  • The code runs, but no value is sent back
def log_message(message):
    print(message)

result = log_message("hi")
print(result)
What will this print?

Functions help you avoid repeating code (DRY — Don't Repeat Yourself).

Examples

Function with default parameter

Default parameter values make arguments optional

def total(price, shipping=5):
    return price + shipping

print(total(20))     # 25 (shipping defaults to 5)
print(total(20, 10)) # 30

How well did you understand this?

Next in Programming Fundamentals

Loops

Continue