AtomLearn
DashboardGoalsGraphAchievementsReviewSign In
Programming FundamentalsNot Started

Functions

Define reusable blocks of code with parameters and return values

0%

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 greet(name):
    return f"Hello, {name}!"

print(greet("Alice")) # Hello, Alice! ```

Key concepts:

  • def keyword starts the definition
  • Parameters are inputs (name above)
  • return sends a value back to the caller
  • Functions with no return statement return None
  • Arguments are the actual values passed when calling

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

Examples

Function with default parameter

Default parameter values make arguments optional

def power(base, exp=2):
    return base ** exp

print(power(3))    # 9 (exp defaults to 2)
print(power(2, 3)) # 8

Next in Programming Fundamentals

Loops

Continue