Functions in Python are defined with def, and every function returns None by default if there's no explicit return.
def greet(name):
'''Return a friendly greeting for the given name.''' # docstring — documents the function
return f"Hello, {name}!"
print(greet("Rahim")) # "Hello, Rahim!"
def no_return():
print("I don't return anything explicitly")
result = no_return() # prints the message, then...
print(result) # None — every function returns None if not told otherwise
def area(width, height):
return width * height
print(area(height=5, width=10)) # keyword arguments — order doesn't matter
The triple-quoted string immediately inside a function body is its docstring — accessible via help(greet) or greet.__doc__, and the standard, idiomatic way to document what a function does, its parameters, and its return value, rather than a # comment above the def line.
Arguments can be passed positionally (matched by order, as usual) or by keyword (matched by name, area(height=5, width=10)), which makes call sites more self-documenting for functions with several parameters, and lets arguments be supplied in any order.
Everything in Python is an object, including functions themselves — a function can be assigned to a variable, passed as an argument, and returned from another function, without any special syntax (covered further in the Lambda and Decorators lessons).