Function Parameters

Concepts covered: Flexible Arguments

Most functions need input: an area function needs the dimensions, and a name formatter needs the name. Parameters are variables that receive values when you call the function. They appear inside the parentheses in the function definition and act as placeholders for the actual values you will provide.

Parameters and arguments get mixed up a lot. They describe the same value, seen once from the definition and once from the call.

# name is a PARAMETER - receives a value
def greet(name):
    print("Hello, " + name + "!")
    print("Nice to meet you, " + name + ".")

# "Alice" is an ARGUMENT
greet("Alice")
print("---")
greet("Bob")
print("---")
greet("Charlie")

Output:

Hello, Alice!
Nice to meet you, Alice.
---
Hello, Bob!
Nice to meet you, Bob.
---
Hello, Charlie!
Nice to meet you, Charlie.

When you call greet("Alice"), Python assigns the value "Alice" to the parameter name. Inside the function, name behaves like any other variable, holding that value for the duration of the function call.

Multiple parameters

Functions can accept multiple parameters, separated by commas. When calling the function, you must provide arguments in the same order as the parameters are defined:

def calculate_area(width, height):
    area = width * height
    print("Width: " + str(width))
    print("Height: " + str(height))
    print("Area: " + str(area))
    print("---")

# Arguments must match parameter order
calculate_area(5, 3)
calculate_area(10, 4)
calculate_area(7, 6)

Output:

Width: 5
Height: 3
Area: 15
---
Width: 10
Height: 4
Area: 40
---
Width: 7
Height: 6
Area: 42
---

The first argument becomes the first parameter, the second argument becomes the second parameter, and so on. This is called positional matching because the position determines which parameter receives which value.

Default parameter values

Sometimes you want a parameter to have a sensible default value that the caller can override if needed. This makes parameters optional. Default values are specified with an equals sign in the function definition:

def greet(name, greeting="Hello"):
    print(greeting + ", " + name + "!")

# Using the default greeting
greet("Alice")

# Providing a custom greeting
greet("Bob", "Hi")
greet("Charlie", "Good morning")
greet("Diana", "Welcome")

Output:

Hello, Alice!
Hi, Bob!
Good morning, Charlie!
Welcome, Diana!

The parameter greeting has a default value of "Hello". When you call greet("Alice") with only one argument, greeting uses its default. When you provide a second argument, that value overrides the default.

A formatting function with defaults

Defaults suit functions where most callers want the same behavior. In this formatting function, most calls use the defaults, and callers customize only when they need to:

def format_price(amount, currency="$", decimals=2):
    formatted = round(amount, decimals)
    return currency + str(formatted)

# Most common use - just the amount
print(format_price(19.99))
print(format_price(100))

# Override currency
print(format_price(50, "EUR "))

# Override both
print(format_price(1234.5678, "GBP ", 3))

Output:

$19.99
$100
EUR 50
GBP 1234.568

Callers who just need dollar formatting with 2 decimals pass only the amount. Callers with special needs can override one or both defaults. This pattern appears throughout Python's standard library.

Wrong number of arguments

If you call a function with too few or too many arguments, Python raises a TypeError. The error message tells you exactly how many arguments were expected versus how many you provided:

def greet(first_name, last_name):
    print("Hello, " + first_name + " " + last_name + "!")

# Correct: provides both required arguments
greet("Ada", "Lovelace")

# Wrong: missing the second argument
# greet("Ada")
# TypeError: missing required argument

# Wrong: too many arguments
# greet("Ada", "Lovelace", "PhD")
# TypeError: takes 2 arguments but 3 given

The message names the function, how many arguments it expects, and how many you provided, which is usually enough to find the mismatch.

Try calling the function below with the defaults, then override them.

Well-chosen parameters let one function serve many callers without edits to its body.

Default values let callers omit arguments they do not need to customize, which cuts boilerplate in the common case.

Parameters without defaults are required; parameters with defaults are optional. Required parameters must always come before optional ones in the function signature so Python can unambiguously match arguments to parameters.

About this lesson

This section is part of the Functional Programming: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.