Functions

How functions are defined for modularity in different languages.

Python Language


Function Definition with Arguments and a Return Type

How to pass arguments to a function and express the return type.

Error Loading

Missing

Function Definition with Arguments and Return Type

Python allows function definitions with optional type hints for parameters and return values.

def add(x: int, y: int) -> int: return x + y def greet(name: str) -> str: return f"Hello, {name}!"
  • x: int, y: int - Type hints for parameters.
  • -> int - Type hint for return value.
  • Type hints are optional; Python does not enforce them at runtime.

Function Overloading

Provide multiple implementations with different sets of arguments.

Missing

Function overloading not supported explicitly, process at runtime:

def add(x, y=None): if y is None: # Single argument case return x return x + y print(add(5, 10)) # 15 print(add(5)) # 5

Or using Union:

from typing import Union def add(x: Union[int, str], y: Union[int, str]) -> Union[int, str]: return x + y print(add(5, 10)) # 15 print(add("Hello, ", "World!")) # "Hello, World!"

Or using @singledispatch:

from functools import singledispatch @singledispatch def process(value): raise NotImplementedError("Unsupported type") @process.register def _(value: int): return f"Processing an integer: {value}" @process.register def _(value: str): return f"Processing a string: {value}" print(process(42)) # "Processing an integer: 42" print(process("Hello")) # "Processing a string: Hello"

Default or Optional Arguments

Provide defaults or express optionality in arguments.

Missing

Function Default Arguments

def greet(name="World"): print(f"Hello, {name}!") greet() # Hello, World! greet("Alice") # Hello, Alice!

Lambda Functions

Define a lambda function.

Missing

add = lambda x, y: x + y print(add(3, 4)) # 7

There are some limitations:

  • Only one expression, no statements like if, for, print.
  • Can hurt readability for anything non-trivial.
  • Can't contain annotations or docstrings.

Lambda Function Capture

Use local in-scope variables inside the lambda function.

Missing

Lambda Functions With Capture

Supported...

x = 10 f = lambda: x + 1 print(f()) # 11 x = 20 print(f()) # 21 — uses the updated value of x

But note the following:

funcs = [lambda: i for i in range(3)] print([f() for f in funcs]) # [2, 2, 2] — not [0, 1, 2] # fix by binding i as a default argument for each iteration funcs = [lambda i=i: i for i in range(3)] print([f() for f in funcs]) # [0, 1, 2]

Function Type Parameter (Generics)

Define a function with one or more types as an argument.

Missing

Function Generics

from typing import TypeVar T = TypeVar('T') # Generic type placeholder def identity(value: T) -> T: return value print(identity(42)) # 42 print(identity("Hello")) # "Hello"

Function Type Parameter Constraints

Define constraints on the type parameter.

Missing

Function Generics with Constraint

With a constraint:

from typing import TypeVar, Protocol class HasLength(Protocol): def __len__(self) -> int: ... T = TypeVar('T', bound=HasLength) def get_length(value: T) -> int: return len(value) print(get_length("Hello")) # 5 print(get_length([1, 2, 3])) # 3