Functions
Defining reusable blocks of code
Introduction
In this lesson, you'll learn about functions in Python. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C, you're familiar with defining reusable blocks of code.
Python has its own approach to defining reusable blocks of code, which we'll explore step by step.
The Python Way
Let's see how Python handles this concept. Here's a typical example:
# No prototype needed — define before use
def add(a: int, b: int) -> int:
return a + b
def greet(name: str) -> None:
print(f"Hello, {name}!")
# Multiple return values (tuple unpacking)
def minmax(arr: list[int]) -> tuple[int, int]:
return min(arr), max(arr)
lo, hi = minmax([3, 1, 4, 1, 5])
# Default arguments
def connect(host: str, port: int = 8080) -> str:
return f"{host}:{port}"
# *args and **kwargs
def log(*messages: str, level: str = "INFO") -> None:
for msg in messages:
print(f"[{level}] {msg}")Comparing to C
Here's how you might have written similar code in C:
#include <stdio.h>
/* Declaration (prototype) */
int add(int a, int b);
void greet(const char *name);
/* Definition */
int add(int a, int b) {
return a + b;
}
void greet(const char *name) {
printf("Hello, %s!\n", name);
}
/* Multiple return values: use output params */
void minmax(int *arr, int n, int *min, int *max) {
*min = *max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < *min) *min = arr[i];
if (arr[i] > *max) *max = arr[i];
}
}You may be used to different syntax or behavior.
Python needs no function prototypes; define before first use
You may be used to different syntax or behavior.
Python returns multiple values as tuples; C uses output pointer parameters
You may be used to different syntax or behavior.
Python supports default arguments natively; C requires workarounds
You may be used to different syntax or behavior.
Python *args/**kwargs replace C's variadic macros (va_list)
Step-by-Step Breakdown
1. No Prototypes
C requires a prototype (declaration) before use if the definition comes later. Python just needs the function defined before it's called.
2. Multiple Return Values
C fakes multiple returns with pointer output parameters. Python returns tuples naturally, and the caller unpacks them.
void minmax(int *arr, int n, int *min, int *max)def minmax(arr: list[int]) -> tuple[int, int]:
return min(arr), max(arr)
lo, hi = minmax(nums)3. Default Arguments
Python functions can have default parameter values, reducing the need for function overloads common in C codebases.
def connect(host: str, port: int = 8080): ...Common Mistakes
When coming from C, developers often make these mistakes:
- Python needs no function prototypes; define before first use
- Python returns multiple values as tuples; C uses output pointer parameters
- Python supports default arguments natively; C requires workarounds
Key Takeaways
- No function prototypes in Python
- Output pointer params → tuple return values
- Python supports default arguments
- *args/**kwargs replace C's variadic functions