Warmup

Write a function join_str that accepts two strings and returns the concatenation of them.

Agenda

  • Lesson 4.2
  • Photo Day
7:15 - 7:35 Last names A - B
7:35 - 7:55 Last names C - D
7:55 - 8:15 Last names E - F
8:15 - 8:45 Last names G - H
9:00 - 9:20 Last names  K - L 
9:20 - 9:40 Last names M - N
9:45 - 10:05 Last names O - P
10:30 - 10:50 Last Names Q-R
10:50 - 11:10 Last Names  S - T
11:10 - 11:30  Last Names V- Z

Reminders

  • Python Essentials Module 4 due this week

    • Module 4 Quiz due Thursday
    • Module 4 Test due Friday
  • No Unit 4 exam

    • Midterm (40 questions) will instead be heavily weighted on Unit 4

Lesson 4.2: Function Parameters

Review: Functions

  • Functions are reusable blocks of code
  • They can take parameters
  • They can have return values using return
    • or they can return None

Review: Functions

  • When functions are called, they are provided with arguments
    • We can pass positional arguments or keyword arguments
def my_func(a, b):
	return a + b

# keyword arguments
result = my_func(b=2, a=10)
print(result)

# positional arguments
result2 = my_func(10, 2)
print(result2)

Review: Functions

  • Writing functions without side effects makes them easier to test
  • Side effect = changing something outside the function (e.g., printing, modifying globals, in-place mutations)
  • In-place mutation = modifying the variable

Why Parameters?

  • Parameters allow us to send information into a function
  • Example:
def greet(name):
    return f"Hello, {name}!"
  • Call: greet("Alex")"Hello, Alex!"

Positional Parameters

  • Parameters filled in order
def power(base, exponent):
    return base ** exponent

print(power(2, 3))   # 8
print(power(3, 2))   # 9
  • Order matters!

Keyword Parameters

  • Use the parameter name when calling
print(power(exponent=3, base=2))  # 8
  • Order doesn’t matter when using keywords

Default Parameters

  • Provide fallback values
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Maya"))              # Hello, Maya!
print(greet("Maya", "Welcome"))   # Welcome, Maya!

Variable-Length Parameters

  • Collect multiple values
  • *args → collects extra positional arguments
  • **kwargs → collects extra keyword arguments
def add_all(*nums):
    return sum(nums)

print(add_all(1,2,3,4))   # 10

Variable-Length Keyword Example

def describe_pet(**info):
    return f"{info['name']} is a {info['type']}."

print(describe_pet(name="Buddy", type="dog"))

Side Effects vs. Return Values

  • Side effect: changes something outside the function

    • e.g., print, modifying global variables, writing files
  • Safer to return values instead

  • Example:

def add(a, b):
    return a + b   # testable

Quick Check

  • What is the difference between positional and keyword arguments?
  • Why are default parameters useful?
  • Why avoid side effects when possible?

Practice Challenge

Write a function average(*nums) that:

  • Accepts any number of numbers
  • Returns their average

Next

  • Do Assignment 4.2