C
PY

C to Python

10 lessons

Progress0%
1Variables & Types2Functions3Arrays → Lists4Structs → Classes & Dicts5Memory Management6String Handling7File I/O8Object-Oriented Programming9Exceptions and Context Managers10Standard Library and Tools
All Mirror Courses
C
PY
Functions
MirrorLesson 2 of 10
Lesson 2

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.

Mirror Card
C
From C:

In C, you're familiar with defining reusable blocks of code.

PY
In Python:

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:

PY
Python 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:

C
C (What you know)
#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];
    }
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

Python needs no function prototypes; define before first use

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

Python returns multiple values as tuples; C uses output pointer parameters

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

Python supports default arguments natively; C requires workarounds

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

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.

Rule of Thumb
In Python, define helper functions before the code that calls them, or put them in modules.

2. Multiple Return Values

C fakes multiple returns with pointer output parameters. Python returns tuples naturally, and the caller unpacks them.

C
C
void minmax(int *arr, int n, int *min, int *max)
PY
Python
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.

PY
Python
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
Common Pitfall
Don't assume Python works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • No function prototypes in Python
  • Output pointer params → tuple return values
  • Python supports default arguments
  • *args/**kwargs replace C's variadic functions
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Python to practice these concepts.
PreviousNext