Functions
Defining functions in C
Introduction
In this lesson, you'll learn about functions in C. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with defining functions in c.
C has its own approach to defining functions in c, which we'll explore step by step.
The C Way
Let's see how C handles this concept. Here's a typical example:
#include <stdio.h>
int add(int a, int b) { return a + b; }
/* No default params */
void greet(const char *name) {
printf("Hello, %s!\n", name);
}
/* Multiple returns: use output parameters */
void minmax(int *arr, int n, int *lo, int *hi) {
*lo = *hi = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < *lo) *lo = arr[i];
if (arr[i] > *hi) *hi = arr[i];
}
}
/* Function pointer instead of first-class function */
typedef int (*UnaryOp)(int);
int apply(UnaryOp fn, int x) { return fn(x); }
int doub(int x) { return x * 2; }
int main() {
int nums[] = {3, 1, 4};
int lo, hi;
minmax(nums, 3, &lo, &hi);
printf("lo=%d hi=%d\n", lo, hi);
printf("%d\n", apply(doub, 5));
}Comparing to Python
Here's how you might have written similar code in Python:
def add(a: int, b: int) -> int:
return a + b
def greet(name: str = "World") -> str:
return f"Hello, {name}!"
def minmax(nums: list) -> tuple:
return min(nums), max(nums)
lo, hi = minmax([3, 1, 4])
# First-class functions
def apply(fn, x):
return fn(x)
double = lambda x: x * 2
print(apply(double, 5))You may be used to different syntax or behavior.
C has no default parameters; Python does
You may be used to different syntax or behavior.
C multiple returns use output pointer parameters; Python returns tuples
You may be used to different syntax or behavior.
C function pointers replace Python first-class functions
You may be used to different syntax or behavior.
C requires prototypes for forward references; Python has no such requirement
Step-by-Step Breakdown
1. Output Parameters
C can't return multiple values directly. Pass pointers to output variables that the function fills in.
def minmax(nums): return min(nums), max(nums)
lo, hi = minmax(nums)void minmax(int *arr, int n, int *lo, int *hi) { ... }
minmax(arr, n, &lo, &hi);2. Function Pointers
C function pointers hold addresses of functions, enabling callbacks like Python's first-class functions — but with verbose typedef syntax.
double = lambda x: x * 2; apply(double, 5)typedef int (*UnaryOp)(int);
int doub(int x) { return x * 2; }
apply(doub, 5);3. No Default Parameters
C has no default parameter values. Provide separate convenience functions instead.
def greet(name="World"): ...void greet(const char *name) { ... }
void greet_default() { greet("World"); }Common Mistakes
When coming from Python, developers often make these mistakes:
- C has no default parameters; Python does
- C multiple returns use output pointer parameters; Python returns tuples
- C function pointers replace Python first-class functions
Key Takeaways
- No default parameters in C
- Multiple returns via output pointer parameters
- Function pointers for first-class function behavior
- Prototypes required for forward references