Functions
Functions
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 functions.
Python has its own approach to functions, which we'll explore step by step.
The Python Way
Let's see how Python handles this concept. Here's a typical example:
# Standalone functions — no class needed
def add(a, b):
return a + b
def greet(name="World"):
return f"Hello, {name}!"
# Lambda (single expression only)
square = lambda x: x * xComparing to C#
Here's how you might have written similar code in C#:
// C# methods must live inside a class
static int Add(int a, int b) => a + b;
static string Greet(string name = "World") {
return $"Hello, {name}!";
}
// Lambda
Func<int, int> square = x => x * x;You may be used to different syntax or behavior.
Python functions are standalone — no containing class required
You may be used to different syntax or behavior.
def keyword replaces the return type + method name syntax
You may be used to different syntax or behavior.
Default parameter values work the same way
You may be used to different syntax or behavior.
Python lambda is limited to a single expression; use def for multi-line
You may be used to different syntax or behavior.
No return type annotation needed (but recommended with type hints)
Step-by-Step Breakdown
1. Defining Functions
Python uses def followed by the function name and parameters. No return type, no access modifier, no static keyword.
public static int Multiply(int a, int b) {
return a * b;
}def multiply(a, b):
return a * b
# With type hints (recommended):
def multiply(a: int, b: int) -> int:
return a * b2. Default and Keyword Arguments
Python default parameters work like C# default values. Python also supports calling by keyword name, which C# only offers via named arguments.
static void Connect(string host, int port = 8080) { }
Connect("localhost");
Connect("localhost", port: 3000); // nameddef connect(host, port=8080):
pass
connect("localhost")
connect("localhost", port=3000) # keyword arg
connect(port=3000, host="localhost") # any order!3. Variable Arguments
*args collects positional arguments into a tuple (like params in C#). **kwargs collects keyword arguments into a dict.
static int Sum(params int[] nums) =>
nums.Sum();def sum_all(*args): # tuple of positional args
return sum(args)
def config(**kwargs): # dict of keyword args
for k, v in kwargs.items():
print(f"{k}={v}")
sum_all(1, 2, 3, 4)
config(host="localhost", port=8080)4. First-Class Functions
Python functions are objects. You can pass them, store them in variables, and return them — same as C# delegates and Func<T>.
Func<int, int> transform = x => x * 2;
List<int> result = list.Select(transform).ToList();transform = lambda x: x * 2
result = list(map(transform, my_list))
# Or pass a named function
def double(x):
return x * 2
result = list(map(double, my_list))Common Mistakes
When coming from C#, developers often make these mistakes:
- Python functions are standalone — no containing class required
- def keyword replaces the return type + method name syntax
- Default parameter values work the same way
Key Takeaways
- def replaces the return-type + method-name syntax
- Functions are standalone — no class required
- Default and keyword arguments work like C# defaults and named args
- *args and **kwargs handle variable argument lists