Technology Functions | Python Tutorial

Learn with diagrams, code, systems and practical examples.

</>
TutorialsPythonFunctions
Functions & Modules · Chapter 15

Functions

Package reusable behavior behind clear inputs and outputs.

Why this matters

Functions help you avoid duplication and give a meaningful name to a unit of work.

Start with the idea

A function can receive parameters, perform work, and return a result. Good functions are small enough to understand and named after what they accomplish.

IdeaUnderstand the purpose
CodeRun the smallest example
PracticeChange it yourself
SubjectVision learning pattern: understand the idea before memorizing syntax.

Small working example

Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.

Python
def calculate_total(price, quantity):
    return price * quantity

print(calculate_total(8, 3))
Expected output
24
What to notice

The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.

Common beginner mistake

Mistake

Printing a value inside a function when callers actually need the value returned.

Better approach

Use return when the result should be reusable by other code; print only when displaying output is the function's job.

Quick recap

  • Functions name reusable behavior.
  • Parameters are inputs.
  • return sends a result back to the caller.

Try it yourself

These are deliberately small. If you can complete them without copying the example, you are ready to continue.

  1. Write a function that calculates tax.
  2. Call the same function with three different inputs.