Knowledge Card - Function Parameters

Function Parameters

  1. Positional Parameters: Parameters passed in a specific order.

    • Example: def add(x, y): return x + y requires x and y in order.
  2. Default Parameters: Parameters with predefined values.

    • Example: def greet(name='World'): print(f'Hello, {name}') allows calling without arguments.
  3. Keyword Arguments: Pass arguments by name for clarity.

    • Example: greet(name='Alice') explicitly specifies the name parameter.
  4. Variable-Length Arguments: Allow functions to accept any number of arguments.

    • Example: def sum_all(*args): return sum(args) accepts multiple numbers.