Knowledge Card - Function Parameters
Function Parameters
-
Positional Parameters: Parameters passed in a specific order.
- Example:
def add(x, y): return x + y
requiresx
andy
in order.
- Example:
-
Default Parameters: Parameters with predefined values.
- Example:
def greet(name='World'): print(f'Hello, {name}')
allows calling without arguments.
- Example:
-
Keyword Arguments: Pass arguments by name for clarity.
- Example:
greet(name='Alice')
explicitly specifies thename
parameter.
- Example:
-
Variable-Length Arguments: Allow functions to accept any number of arguments.
- Example:
def sum_all(*args): return sum(args)
accepts multiple numbers.
- Example: