Functions — Basics
Define and call reusable functions. Read the lesson first, then move through the exercises in order.
After reading
Practice Arena
Begin with the first exercise, then continue step by step through the module.
Start with Greeting FunctionStudy Material
Read the full lesson
The problem with copy-pasting
Imagine you are building a game, and every time the player levels up, you want to show a fireworks animation and play a sound.
If you write that code 20 times across your program, what happens when you decide to change the sound? You have to hunt down and fix 20 different places in your code.
This is a nightmare.
Good programmers hate repeating themselves. Instead, they use functions.
A function is a block of code that you write once, give a name, and then reuse as many times as you want.
Defining your first function
To create a function, you use the def keyword (short for "define").
pythondef say_hello(): print(\"Welcome to the game!\") print(\"Get ready to play.\")
That code tells Python how to say hello, but it does not actually run the code yet. It just stores the instructions in a box labeled say_hello.
Calling the function
To actually run the code inside the box, you must call the function by writing its name followed by parentheses ().
pythonsay_hello() # Output: # Welcome to the game! # Get ready to play.
You can call it 5 times, or 500 times. The code only needs to exist once.
Passing data in with parameters
Functions become extremely powerful when you can pass data into them.
We do this using parameters. Think of a parameter as a temporary variable that catches data you toss into the function.
pythondef greet(name): print(f\"Hello there, {name}!\")
Now, when you call the function, you must provide a value for name. That value is called an argument.
pythongreet(\"Alice\") # Alice is the argument greet(\"Bob\") # Bob is the argument
This makes the function flexible. It does the same core action, but adapts based on the input.
Multiple parameters
You can give a function as many parameters as you want, separated by commas.
pythondef describe_person(name, age): print(f\"{name} is {age} years old.\") describe_person(\"Alice\", 25)
When you pass arguments like this, Python matches them up entirely by their order. \"Alice\" goes into name because it is first. 25 goes into age because it is second.
These are called positional arguments.
Keyword arguments
Sometimes, relying on the order gets confusing, especially if a function has 5 or 6 parameters.
Instead of remembering the order, you can explicitly name the arguments when you call the function.
pythondescribe_person(age=25, name=\"Alice\")
These are called keyword arguments. The order doesn't matter anymore, because Python knows exactly where each piece of data belongs. This makes the code calling the function very obvious and easy to read.
Making parameters optional
What if you want a parameter to have a default value in case the user forgets to provide one?
You can set a default value directly in the function definition.
pythondef order_coffee(size, kind=\"Latte\"): print(f\"Here is your {size} {kind}.\") order_coffee(\"Large\") # Uses the default \"Latte\" order_coffee(\"Small\", \"Mocha\") # Overrides the default with \"Mocha\"
Because kind has a default value, it becomes completely optional when calling the function.
Important rule: Optional parameters must always be listed after all required parameters in the definition.
What this lesson should give you
After this lesson, you should understand how to:
- avoid repetitive code by defining your own functions with
def - run a function's code by calling its name with parentheses
() - pass data into a function using parameters and arguments
- understand the difference between positional and keyword arguments
- create optional parameters by assigning default values
Functions are the building blocks of all professional software. By breaking large problems into small, reusable functions, you make your programs clean, organized, and powerful.
Interactive
Exercises for this topic
These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.
Greeting Function
Complete the "Greeting Function" exercise.
Default Parameters
Complete the "Default Parameters" exercise.
Rectangle Perimeter
Complete the "Rectangle Perimeter" exercise.
Absolute Value Function
Complete the "Absolute Value Function" exercise.
Positional vs Keyword Args
Complete the "Positional vs Keyword Args" exercise.