Function Parameters

Carson West

Python 1 Home

Function Parameters

Python function parameters allow you to pass data into functions, making them reusable and flexible. There are several types of parameters:

def greet(name, greeting):
  print(f"{greeting}, {name}!")

greet("Alice", "Hello") # Output: Hello, Alice!
greet(greeting="Good morning", name="Bob") # Output: Good morning, Bob!
def greet(name, greeting="Hello"):
  print(f"{greeting}, {name}!")

greet("Charlie") # Output: Hello, Charlie!
greet("Dave", "Hi") # Output: Hi, Dave!

Default Parameters

def sum_all(*args):
  total = 0
  for num in args:
    total += num
  return total

print(sum_all(1, 2, 3)) # Output: 6
print(sum_all(10, 20, 30, 40)) # Output: 100
def print_kwargs(**kwargs):
  for key, value in kwargs.items():
    print(f"{key}: {value}")

print_kwargs(name="Eve", age=30, city="[New York](./../new-york/)")
def my_function(a, b, *args, c=5, **kwargs):
  print(f"a: {a}, b: {b}, args: {args}, c: {c}, kwargs: {kwargs}")

my_function(1, 2, 3, 4, c=10, d=20)
def annotated_function(name: str, age: int) -> str:
  return f"{name} is {age} years old."

Type Hinting

Related Notes: Python Functions, Return Values