Skip to main content

Building with functions

Functions are reusable blocks of code that do specific tasks. Instead of writing the same code multiple times, you write it once as a function and call it whenever needed. Think of functions like:
  • A recipe you can follow multiple times
  • A machine that takes input and produces output
  • A named shortcut for complex operations

Why use functions?

  • Don’t repeat yourself: Write code once, use it many times
  • Stay organized: Break complex programs into smaller pieces
  • Fix bugs easier: Change code in one place, affects everywhere
  • Test your code: Test each function separately

What you’ll learn

Real-world examples

Functions are everywhere in programming:
  • print() - displays text (built-in function)
  • len() - counts items (built-in function)
  • calculate_tax() - your custom function
  • send_email() - your custom function

Let’s begin!

Start by learning how to create your first function.

Defining functions

Create your first function