Skip to chapter content
navan✳THE BUILD LABPYTHON / GUIDE
0/26 read
CHAPTER 04 / 26 · FOUNDATIONS

Functions and reusable ideas.

Turn a clear input-output rule into a named function you can trust.

BY NAVAN SAMI5 MIN READTHEORY FIRST
01 / THE BASICS

Start here.

A function packages instructions so a program can use them more than once. def line_total(price, quantity=1): defines a function named line_total. The names inside parentheses are parameters: local names for values supplied when the function is called. The colon starts its indented body. Defining the function does not run the body. The call line_total(7, 3) runs it with price equal to 7 and quantity equal to 3.

The return statement sends a result back to the caller and ends that call. For line_total(7, 3), the multiplication produces 21, so the call expression has value 21. print(line_total(7, 3)) then displays that returned value. A function that only prints may look similar on the screen, but its caller receives None unless it also returns. Returning a value lets later code calculate with it, store it, or pass it to another function.

The =1 in the parameter list is a default. If a caller supplies only a price, quantity becomes 1 for that call. Another call can supply a different quantity; calls do not overwrite the function's definition. A useful function has a clear contract: what inputs it expects, what result it returns, and what it does with unusual inputs. The example expects numeric price and quantity values. If a caller supplies text instead, the operation may behave differently or fail, so the contract should be understood before reuse.

02 / THE IDEA

Build the model.

A function gives a reusable piece of behavior a name. You define it with def, list parameters inside parentheses, indent its body, and call it with arguments. A return statement ends the call and sends a value back to the caller. Printing inside a function is different: it displays something but does not hand that value back for another calculation.

Think of a function as a small contract. What inputs does it accept? What result will it produce? What happens for an empty, missing, or invalid input? A function that answers these questions is easier to test and reuse than a block of code copied into several places. Good names describe outcomes or actions rather than how many lines the body contains.

03 / UNDER THE SURFACE

How it works.

Parameters are local names for the values supplied at a call site. Positional arguments are matched by position; keyword arguments name the parameter explicitly. A default value lets a caller omit an argument, but avoid a mutable list or dictionary as a default because it is created once and shared across calls. Use None and create a new container inside the function instead.

A function can call another function, and each call has its own local scope. It may read an outer name, but hidden dependencies on globals make testing harder. Pass important data in as arguments and return the useful result. This keeps the data flow visible and lets you try the function with small examples before connecting it to files or network requests.

04 / A WORKED EXAMPLE

Read it line by line.

example.py
def line_total(price, quantity=1):
    return price * quantity

print(line_total(7, 3))
print(line_total(7))

The first call binds 7 to price and 3 to quantity, multiplies them, and returns 21. The second call binds 7 to price and uses the default quantity 1, returning 7. print displays each returned value. The function itself remains usable inside a larger expression because it returns rather than only printing.

05 / ANOTHER EXAMPLE

See the rule again.

example-2.py
def discounted(price, percent):
    return price * (1 - percent / 100)

print(discounted(80, 25))

The output is 60.0. Dividing the percentage by 100 turns 25 into 0.25; subtracting from one leaves the fraction to pay. A production function should decide whether negative or above-100 percentages are allowed.

06 / REMEMBER THIS

A mistake to avoid.

A function with no return statement returns None. If you later try to add that result to a number, the error appears at the caller even though the missing return is inside the function.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗Python · Understand the idea, then write the code.