Function Examples¶
These examples reinforce the concepts from the previous sections: parameters, return values, type hints, and composition.
Square¶
A function with a single parameter and a return value.
def square(x: int) -> int:
return x * x
print(square(5))
Output
25
Rectangle Area¶
A function with multiple parameters.
def area(length: float, width: float) -> float:
return length * width
print(area(3.0, 4.0))
Output
12.0
Format Name¶
Functions work with any data type, not just numbers.
def format_name(first: str, last: str) -> str:
return last.upper() + ", " + first
print(format_name("Alice", "Smith"))
print(format_name("Bob", "Lee"))
Output
SMITH, Alice
LEE, Bob
Temperature Conversion¶
A function that performs a real calculation.
def celsius_to_fahrenheit(c: float) -> float:
return (c * 9 / 5) + 32
print(celsius_to_fahrenheit(25))
Output
77.0
Maximum Value¶
Functions can include conditional logic.
def max_value(a: int, b: int) -> int:
if a > b:
return a
return b
print(max_value(10, 4))
Output
10
Composition¶
Functions can be combined by passing the return value of one function as the argument to another.
def square(x: int) -> int:
return x * x
def double(x: int) -> int:
return 2 * x
print(double(square(3)))
square(3) runs first and returns 9.
That value is then passed to double, which returns 18.
Output
18
Key Ideas¶
This page concludes the functions section.
A function is a black box that optionally takes inputs and optionally produces an output.
Parameters let a function accept different values each time it is called, and the return statement sends a result back to the caller.
Type hints document the expected types without changing how Python runs the code.
Small functions can be composed — the return value of one becomes the argument of another — to build larger programs from simple, reusable pieces.
Next: Runtime Model (Call Stack).