Functions are reusable blocks of code that perform a specific task. They make programs organised, modular, and less repetitive. Python has built-in functions and allows you to create your own.
Syntax: def greet(name): print(f"Hello, {name}!"). Call: greet("Alice"). Parameters (in definition) vs arguments (in call). Return value: def add(a, b): return a + b. result = add(3, 5) → 8. Multiple return values: return x, y. Default parameters: def power(base, exp=2): — exp defaults to 2 if not provided. Functions without return implicitly return None.
Scope: where a variable is accessible. Local: defined inside function — exists only during function execution. Global: defined outside — accessible everywhere (but use global keyword to modify inside function). Built-in functions: print(), input(), len(), type(), int(), float(), str(), range(), abs(), round(), max(), min(), sum(), sorted(), enumerate(). Module functions: import math; math.sqrt(16), math.pi. import random; random.randint(1,6).
Functions provide: (1) Reusability — write once, use many times. (2) Modularity — break complex programs into manageable pieces. (3) Readability — descriptive function names make code self-documenting. (4) Debugging — if something breaks, you know which function has the bug. (5) Testing — test each function independently. (6) Avoid repetition (DRY: Don't Repeat Yourself). Even for code used just twice, a function is usually cleaner.
Book a Trial + Diagnostic session. Get a personalized Learning Path with clear milestones, tutor match, and a plan recommendation — all within 24 hours.
Book Trial + Diagnostic →