Chapter 3

Functions and Modules

Package reusable logic, manage data, and organize code.

Defining functions

Functions name a reusable block of work. They help you stop copying the same instructions into several places and give a piece of logic a clear name.

Once a function has a name, you can think about the behavior as a single unit. That makes larger programs easier to follow because you can read the names first and the details later.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Type `def greet():` with `print("Hello")`, then call `greet()`.
EXERCISE
Output will appear here.
Change the printed word to `Welcome`.
EXERCISE
Output will appear here.
Predict what happens if you define a function but do not call it.
EXERCISE
Output will appear here.
Write `show_color()` that prints one color when called.
EXERCISE
Output will appear here.

Parameters

Parameters let a caller supply different input each time. This is how one function can behave differently without needing a new copy for every case.

When you read a function with parameters, imagine a blank space that will be filled in by the caller. The function definition says what kind of input it expects, and the call supplies the actual value.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Write `def greet(name):` that prints the name.
EXERCISE
Output will appear here.
Call it once with `"Ari"` and once with `"Bea"`.
EXERCISE
Output will appear here.
Predict the output of `greet("Sam")`.
EXERCISE
Output will appear here.
Write `show_age(age)` that prints the given age.
EXERCISE
Output will appear here.

Return values

return sends a result back to the caller. A function with a return value can be used inside a bigger expression, stored in a variable, or passed straight into another function.

This is a key step in writing reusable code: one part does the work, and another part decides what to do with the result.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Write `def double(number):` that returns `number * 2`.
EXERCISE
Output will appear here.
Change the call from `double(4)` to `double(7)`.
EXERCISE
Output will appear here.
Predict `double(0)` before running it.
EXERCISE
Output will appear here.
Write `add(a, b)` that returns their sum, then print `add(2, 3)`.
EXERCISE
Output will appear here.

Default arguments

Defaults make an argument optional. They let you write a function that has a normal case built in, while still allowing the caller to override it when needed.

This is especially handy when one argument has a common value. Instead of repeating the same value every time, you let the function supply it unless the caller asks for something different.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Write `def greet(name, ending=".")` and return the greeting.
EXERCISE
Output will appear here.
Call it once with the default and once with `ending="!"`.
EXERCISE
Output will appear here.
Predict which ending is used by `greet("Ari")`.
EXERCISE
Output will appear here.
Write `describe(item, color="blue")` and print two calls.
EXERCISE
Output will appear here.

Scope

Variables created inside a function stay local to it. Scope keeps names from colliding with each other and helps each function manage its own small area of responsibility.

If a name appears both outside and inside a function, Python treats them as separate values unless you explicitly pass information in or return it out.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Copy the example with `message` outside and inside a function.
EXERCISE
Output will appear here.
Change only the inside message.
EXERCISE
Output will appear here.
Predict what the final outside `print(message)` shows.
EXERCISE
Output will appear here.
Make a function with a local `number` and print a different outside `number`.
EXERCISE
Output will appear here.

Small helpers

Compose small functions to make code easier to test and read. A helper that does one job is easier to understand than one large function that tries to do everything at once.

When you split a task into small helpers, you also make it easier to spot where a mistake lives. You can check each piece on its own instead of searching through a long block of code.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Write `is_even(number)` that returns `number % 2 == 0`.
EXERCISE
Output will appear here.
Call it with 4, then with 5.
EXERCISE
Output will appear here.
Predict the result for 0.
EXERCISE
Output will appear here.
Write `is_positive(number)` and use it inside another small function.
EXERCISE
Output will appear here.