Python Programming · Week 7 notes
Functions
Function Overview
What is a Function in Python?
A function is a reusable block of code that performs a specific task. Functions help organize code, make it more readable, and reduce repetition.
Why Use Functions?
- Reuse code: Write once, use many times
- Organize code: Break down complex problems into smaller parts
- Make code easier to read and understand
Basic Structure of a Function
def function_name():
# Function body
# Code to be executed
print("Hello from the function!")
# Calling the function
function_name()
Example: A Simple Greeting Function
def say_hello():
print("Hello, World!")
# Calling the function
say_hello() # Output: Hello, World!
say_hello() # Output: Hello, World!
In this example, we defined a function called say_hello() that prints "Hello, World!" when called. We can call this function multiple times to reuse the code.
Functions with Parameters
Functions with Parameters
Functions can accept input values called parameters. These allow you to pass information to the function to work with.
Basic Syntax
def function_name(parameter1, parameter2):
# Function body
# Code that uses the parameters
Example: Greeting Function with a Name Parameter
def greet(name):
print(f"Hello, {name}!")
# Calling the function with different arguments
greet("Alice") # Output: Hello, Alice!
greet("Bob") # Output: Hello, Bob!
Multiple Parameters
Functions can have multiple parameters separated by commas.
def describe_pet(animal_type, pet_name):
print(f"I have a {animal_type} named {pet_name}.")
describe_pet("dog", "Buddy") # Output: I have a dog named Buddy.
describe_pet("cat", "Whiskers") # Output: I have a cat named Whiskers.
Parameters make functions more flexible and reusable. You can pass different values to the same function to get different results.
Functions that Return Values
Functions that Return Values
Functions can return values using the return statement. This allows a function to compute a result and give it back to the caller.
Basic Syntax
def function_name(parameters):
# Function body
# Compute something
return result
Example: A Function that Adds Two Numbers
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print(result) # Output: 8
# You can also use the returned value directly
print(add_numbers(10, 20)) # Output: 30
Using Returned Values
You can use the returned value in various ways:
- Assign it to a variable
- Use it in another calculation
- Pass it to another function
- Print it directly
Example: Temperature Converter
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
# Convert 25°C to Fahrenheit
temp_f = celsius_to_fahrenheit(25)
print(f"25°C is equal to {temp_f}°F") # Output: 25°C is equal to 77.0°F
# Use the function in a condition
if celsius_to_fahrenheit(30) > 85:
print("It's a hot day!")
else:
print("It's not too hot today.")
Functions that return values are powerful tools in programming. They allow you to perform calculations or operations and use the results in other parts of your code.