Welcome to this exciting lesson where we’ll explore the power of functions in Python. Functions allow us to organize our code, make it modular, and reuse it efficiently. In this lesson, we’ll cover the basics of defining and calling functions, understanding parameters, and utilizing return values.
Defining and Calling Functions:
1. Defining a Function:
- Functions are defined using the
def
keyword, followed by the function name and parameters. - Syntax:
def function_name(parameter1, parameter2, ...):
# Code to be executed
2. Calling a Function:
- Once a function is defined, it can be called by using its name followed by parentheses.
- Syntax:
function_name(argument1, argument2, ...)
Parameters and Arguments:
3. Parameters:
- Parameters are placeholders in the function definition that receive values when the function is called.
- Example:
def greet(name):
print(f"Hello, {name}!")
# Calling the function with an argument
greet("Alice")
4. Arguments:
- Arguments are the actual values passed to the function when it is called.
- Example:
def add_numbers(num1, num2):
result = num1 + num2
return result
# Calling the function with arguments
sum_result = add_numbers(5, 7)
Return Values:
5. Return Statement:
- The
return
statement is used to send a value back from the function. - Example:
def multiply(num1, num2):
result = num1 * num2
return result
# Calling the function and storing the result
product = multiply(3, 4)
6. Multiple Return Values:
- Functions can return multiple values using tuples.
- Example:
def divide_and_remainder(dividend, divisor):
quotient = dividend // divisor
remainder = dividend % divisor
return quotient, remainder
# Unpacking multiple return values
result_quotient, result_remainder = divide_and_remainder(10, 3)
Example:
Let’s consider an example where we define a function to check if a given number is even or odd:
# Example Code
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
# Calling the function
result = check_even_odd(7)
print(f"The number is {result}.")
Practice Exercise:
Create a Python script that defines a function to calculate the area of a circle. The function should take the radius as a parameter and return the area. Call the function with different radius values and print the results.
# Example Practice Exercise
import math
def calculate_circle_area(radius):
area = math.pi * radius ** 2
return area
# Calling the function with different radius values
radius1 = 5
radius2 = 8
area1 = calculate_circle_area(radius1)
area2 = calculate_circle_area(radius2)
print(f"The area of a circle with radius {radius1} is {area1}.")
print(f"The area of a circle with radius {radius2} is {area2}.")
Summary:
In this lesson, we’ve explored the fundamentals of functions in Python. Understanding how to define and call functions, work with parameters, and handle return values is crucial for building modular and reusable code. Practice creating functions to enhance your coding skills, and don’t hesitate to ask questions in the discussion forum. Let’s continue our Python journey by harnessing the power of functions!