Functions

A function in Python is a block of code that performs a specific task. You can “call” the function whenever you need to use that task, instead of writing the same code again and again​.

It’s like a mini-program inside your main program - it keeps your code organised, reusable and easier to understand​.

Example 1 - Creating a Function

1def sayHello():
2    firstName = input("What is your first name? ")
3
4    print(f"Hello, {firstName}!")
5    print("How are you today?")
6
7sayHello()
Show Output
What is your first name? John
Hello, John!
How are you today?

Explanation

The first line defines a function called sayHello.

The second line asks the user to type in their first name. Whatever the user enters is stored in a variable called firstName.

The fourth line displays a message that includes the user’s name. The f-string lets the variable firstName appear inside the message.

The fifth line displays another friendly message on the screen.

The final line calls the function which means Python runs all the code inside it.

Example 2 - Creating a Function with a Single Parameter

1def greet(firstName):
2    print(f"Greetings, {firstname}!")
3
4greet("John")
Show Output

Greetings, John!

Explanation

This code defines a function called greet that takes one parameter named firstName. Inside the function, the print statement uses an f-string to combine the word “Greetings,” with the value stored in firstName. When the function is called using greet("John"), the word “John” is passed into the function as the value of firstName.

Example 3 - Creating a Function with Multiple Parameters

1def greet(firstName, lastName):
2    print(f"Greetings, {firstName} {lastName}!")
3
4greet("John", "Doe")
Show Output

Greetings, John Doe!

Explanation

This code defines a function called greet that takes two parameters, firstName and lastName. Inside the function, the print statement uses an f-string to combine the word “Greetings,” with both the first and last names. When the function is called using greet("John", "Doe"), the values “John” and “Doe” are passed into the function and replace the parameters.

Example 4 - Creating a Function with a Default Parameter

1def sayGoodbye(firstName="Unknown"):
2    print(f"Goodbye, {firstName}!")
3    print("It was nice to see you.")
4
5sayGoodbye()
6sayGoodbye("Bob")
Show Output
Goodbye, Unknown!
It was nice to see you.
Goodbye, Bob!
It was nice to see you.

Explanation

This code defines a function called sayGoodbye that takes one parameter named firstName. The parameter has a default value of "Unknown", which means if no name is given when the function is called, it will automatically use the word “Unknown.” Inside the function, there are two print statements. The first one uses an f-string to display a message saying goodbye to the person, and the second one prints an extra message saying “It was nice to see you.”

Example 5 - Creating a Function That Performs a Calculation

1def add(number1, number2):
2    result = number1 + number2
3
4    print(f"{number1} + {number2} = {result}")
5
6add(10, 5)
Show Output

10 + 5 = 15

Explanation

This code defines a function called add that takes two parameters, number1 and number2. Inside the function, a new variable called result is created to store the sum of the two numbers. The print statement then uses an f-string to display the full calculation and its result in a clear format. When the function is called using add(10, 5), the values 10 and 5 are passed into the function, so result becomes 15.

Return Values

When a function in Python finishes running, it can send a result back to the part of the program that called it. This is called returning a value​.

You can think of it like the function doing some work, then handing the answer back to you so you can use it somewhere else in your program​.

Example 6 - Creating a Function That Returns a Value

1def add(a, b):
2    return a + b
3
4result = add(10, 5)
5
6print(result)
Show Output

15

Explanation

This code defines a function called add that takes two parameters, a and b. Inside the function, the return statement is used to send back the sum of the two numbers. When the function is called with add(10, 5), it calculates 10 + 5 and returns the value 15. This result is then stored in a variable called result.