Chapter 5 of 11
~13 minFunctions
Functions package up reusable logic behind a name, so it can be called wherever needed instead of duplicated. Understanding parameters vs. arguments, return values, defaults, and lambdas covers most of what you need to read and write real Python functions.
Defining Functions: Parameters vs. Arguments
You define a function with def, followed by a name and a parenthesized list of parameters - the variable names the function expects to receive. When you call the function, the actual values you pass in are called arguments. The distinction is mostly one of vocabulary: parameters describe the function's definition, arguments describe a specific call to it.
Try it yourself
a and b are parameters; 5 and 7 are argumentsdef add(a, b): # a, b are parameters return a + b result = add(5, 7) # 5, 7 are arguments print(result)
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
The return Statement
return sends a value back to whatever code called the function, and immediately ends the function's execution at that point - any code after a return statement inside the same block never runs. A function with no explicit return statement (or a bare return with no value) implicitly returns None.
Try it yourself
A function that returns a value vs. one that returns nothingdef square(x): return x * x def log_message(msg): print(msg) # no return statement -> implicitly returns None value = square(4) print(value) result = log_message("hi") print(result)
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
Default Arguments and Keyword Arguments
A default argument is a parameter given a value in the function definition itself, which is used automatically whenever the caller omits that argument. Arguments can also be passed by keyword (name=value) instead of by position, which lets a caller specify only the ones they care about and in whatever order they like, as long as they name them.
Try it yourself
Defaults plus keyword arguments in different ordersdef greet(name="Guest", greeting="Hello"): return f"{greeting}, {name}!" print(greet()) print(greet("Alice")) print(greet(name="Bob", greeting="Hi")) print(greet(greeting="Hey", name="Carl"))
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
Lambda Functions
A lambda is a small, anonymous function defined inline with the lambda keyword, restricted to a single expression whose value is returned automatically - there is no explicit return keyword. Lambdas are best suited to short, throwaway logic passed directly into another function, like a custom sort key; for anything with real logic or multiple statements, a normal def is clearer and easier to debug.
Try it yourself
A lambda used standalone and as a sort keysquare = lambda x: x * x print(square(5)) pairs = [(1, "b"), (3, "a"), (2, "c")] pairs_sorted = sorted(pairs, key=lambda pair: pair[1]) print(pairs_sorted)
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
Coding challenge
Write a function apply_discount(price, discount_percent=10) that returns the discounted price, defaulting to a 10 percent discount. Apply it to a list of prices using map() with the default discount, then again using a list comprehension with a custom 25 percent discount, and print both results.
# Write your solution here
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
Answer every question below to mark this chapter as complete.
Quiz Yourself
Score: 0/51. In "def add(a, b): return a + b" followed by "add(5, 7)", what are a and b called, and what are 5 and 7 called?
2. What does a function return if it has no explicit return statement?
3. What is the purpose of a default argument like "def greet(name=\"Guest\")"?
4. What is the main limitation of a lambda compared to a regular function defined with def?
5. Given "def greet(name=\"Guest\", greeting=\"Hello\")", what does greet(greeting="Hey", name="Carl") produce?