Functions
Python functions are defined with the def keyword. They can accept parameters (including default values), and return a value with return — or return nothing at all (None) if no return is used.
💡 Key insight
A Python function with no explicit return statement automatically returns None.
Syntax
syntax
def functionName(param1, param2=defaultValue):
# code
return valueExample
Calling add(4, 7) runs the function body and gives back the value after return.
py
def add(a, b):
return a + b
result = add(4, 7)
print("Sum:", result)outputSum: 11
Try it yourself
Copy this example into the Python compiler and run it — try changing a value and see what happens.
