Theory

Functions are reusable blocks of code.

Create a function with def:

def say_hello():
print("Hello!")

# Call the function
say_hello() # Prints: Hello!

Functions with parameters:

def greet(name):
print("Hello, " + name + "!")

greet("Alice") # Prints: Hello, Alice!

Functions that return values:

def add(a, b):
return a + b

result = add(3, 5) # result is 8

Exercises in this lesson

3

Return Value

Create a function 'double' that takes a number and returns it multiplied by 2. Call it with 5 and print the result. Expected output: 10

Medium Points: 20
4

Add Two Numbers

Create a function 'add' that takes two numbers and returns their sum. Call: add(10, 20) and print the result. Expected output: 30

Medium Points: 20
5

Calculator Function

Create a function 'multiply' that takes two numbers and returns their product. Test it with: multiply(6, 7) Expected output: 42

Medium Points: 20
Solve in the playground
Next lesson: Dictionaries →