FUNCTIONS · 9 MIN
Functions
A function is a named, reusable block that takes inputs and returns a result.
PYRA explains
- Define with def name(params): and send a value back with return.
- Default parameters make arguments optional: def greet(name, msg="Hi").
- Variables created inside a function are local — they vanish when it returns.
Real world: A juicer: put fruit in (arguments), get juice out (return value), reuse it every morning.
Code
def area(w, h=1):
return w * h
print(area(4, 3), area(5))
square = lambda x: x * x
print(list(map(square, [1, 2, 3])))Output ›
12 5
[1, 4, 9]Try it in the online compiler →Checkpoint quiz
A function with no return statement returns…
area(5) with def area(w, h=1) gives…