PY
C

Python to C

10 lessons

Progress0%
1Variables & Types2Functions3Lists → Arrays4Memory Management5String Handling6Structs7Preprocessor8File I/O9Pointers and Manual Memory10Build System: Makefile
All Mirror Courses
PY
C
Functions
MirrorLesson 2 of 10
Lesson 2

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.

Mirror Card
PY
From Python:

In Python, you're familiar with defining functions in c.

C
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:

C
C 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:

PY
Python (What you know)
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))
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C has no default parameters; Python does

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C multiple returns use output pointer parameters; Python returns tuples

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C function pointers replace Python first-class functions

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

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.

PY
Python
def minmax(nums): return min(nums), max(nums)
lo, hi = minmax(nums)
C
C
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.

PY
Python
double = lambda x: x * 2; apply(double, 5)
C
C
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.

PY
Python
def greet(name="World"): ...
C
C
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
Common Pitfall
Don't assume C works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in C to practice these concepts.
PreviousNext