Back to the main roadmap | Previous lesson: Exceptions
A function is a reusable block of code that performs a specific task.
Instead of writing the same code multiple times, you write it once inside a function and call it whenever you need it.
Think of a coffee machine.
You don't rebuild the machine every time you want coffee.
You simply press a button, and the machine performs the same steps every time:
- Grind the beans
- Brew the coffee
- Pour the coffee
Functions work the same way.
Use the def keyword to create a function.
def make_coffee():
print("Grinding beans...")
print("Brewing...")
print("Coffee is ready!")
make_coffee()
make_coffee()Grinding beans...
Brewing...
Coffee is ready!
Grinding beans...
Brewing...
Coffee is ready!
def make_coffee():def→ Defines a function.make_coffee→ Function name.()→ Parentheses for inputs (parameters).:→ Starts the function body.- Indented lines → Code that belongs to the function.
A function does nothing until you call it.
Functions become much more useful when they can receive data.
def greet(name):
print("Hello, " + name + "!")
greet("Alex")
greet("Sam")Hello, Alex!
Hello, Sam!
nameis a parameter."Alex"and"Sam"are arguments.
Think of the parameter as an empty box that gets filled when the function is called.
Functions can accept more than one input.
def introduce(name, age):
print(name + " is " + str(age) + " years old.")
introduce("Alex", 10)Alex is 10 years old.
Arguments are matched by their position.
Instead of relying on position, you can specify parameter names.
introduce(age=10, name="Alex")The output is exactly the same.
Using keyword arguments makes code easier to read and allows the arguments to be written in any order.
This is one of the most important concepts in Python.
Displays information on the screen.
def add_print(a, b):
print(a + b)
add_print(3, 5)8
The value is displayed but not returned.
Returns a value back to the caller.
def add(a, b):
return a + b
result = add(3, 5)
print(result)8
The returned value can be stored in a variable or used later.
def add_print(a, b):
print(a + b)
def add_return(a, b):
return a + b
x = add_print(3, 5)
y = add_return(3, 5)
print(x)
print(y)8
None
8
add_print()only displays the value.- It doesn't return anything.
- Python automatically returns
Nonewhen there is noreturnstatement. add_return()returns the value8.
Remember:
print()showsreturngives back
When Python reaches a return statement, the function ends immediately.
def check_age(age):
if age < 0:
return "Invalid age"
return "Valid age"
print("This never runs.")The last print() is never executed because the function has already finished.
Parameters can have default values.
def greet(name="friend"):
print("Hello, " + name + "!")
greet()
greet("Alex")Hello, friend!
Hello, Alex!
If no argument is supplied, Python uses the default value.
Sometimes you don't know how many arguments will be passed.
Use *args.
def add_all(*numbers):
total = 0
for n in numbers:
total += n
return total
print(add_all(1, 2, 3))
print(add_all(1, 2, 3, 4, 5))6
15
*numbers collects all positional arguments into a tuple.
Sometimes you don't know the names of the arguments either.
Use **kwargs.
def describe_person(**info):
for key, value in info.items():
print(key + ":", str(value))
describe_person(
name="Alex",
age=10,
city="Delhi"
)name: Alex
age: 10
city: Delhi
**kwargs stores named arguments inside a dictionary.
Variables created inside a function only exist inside that function.
def my_function():
x = 5
print(x)
my_function()
print(x)5
NameError: name 'x' is not defined
x is a local variable.
It disappears after the function finishes.
Variables created outside functions are called global variables.
count = 0
def increase():
global count
count += 1
increase()
print(count)1
The global keyword tells Python to use the variable outside the function.
Python includes many useful functions that are always available.
| Function | Purpose | Example |
|---|---|---|
print() |
Display output | print("Hello") |
input() |
Read user input | input("Name: ") |
len() |
Count items | len("hello") → 5 |
type() |
Get data type | type(5) |
int() |
Convert to integer | int("25") |
float() |
Convert to float | float("3.14") |
str() |
Convert to string | str(25) |
max() |
Largest value | max(2, 5, 1) |
min() |
Smallest value | min(2, 5, 1) |
sum() |
Sum numbers | sum([1,2,3]) |
sorted() |
Sort items | sorted([3,1,2]) |
range() |
Generate numbers | range(5) |
abs() |
Absolute value | abs(-5) |
You don't need to memorize them all.
You'll naturally learn them as you continue programming.
Create a file named lesson6.py.
def calculate_area(length, width=1):
area = length * width
return area
# Basic call
print(calculate_area(5, 3))
# Using the default value
print(calculate_area(5))
def describe_student(name, **details):
print(f"Student: {name}")
for key, value in details.items():
print(f" {key}: {value}")
describe_student(
"Alex",
age=10,
grade=5,
favorite_subject="Math"
)
def safe_divide(a, b):
try:
return a / b
except ZeroDivisionError:
return "Cannot divide by zero"
print(safe_divide(10, 2))
print(safe_divide(10, 0))
total = 0
def add_to_total(x):
global total
total += x
add_to_total(5)
add_to_total(10)
print(total)15
5
Student: Alex
age: 10
grade: 5
favorite_subject: Math
5.0
Cannot divide by zero
15
- Functions let you reuse code.
- Use
defto create a function. - Call a function using its name followed by
(). - Parameters receive values from arguments.
returnsends a value back.print()displays output but does not return it.- A function ends immediately when it reaches
return. - Default parameters provide fallback values.
*argsaccepts multiple positional arguments.**kwargsaccepts multiple named arguments.- Local variables exist only inside functions.
- Global variables exist outside functions and can be modified using
global. - Python provides many built-in functions like
print(),len(),sum(), andsorted().