Python Tutorial: Creating and Calling Functions
This tutorial introduces you to the fundamental concept of functions in Python. Functions are reusable blocks of code that perform specific tasks. They promote code organization, readability, and reduce redundancy.
What are Functions?
Think of a function as a mini-program within your program. You define it once, and then you can call it multiple times with different inputs to achieve different results. This is similar to creating a subroutine or method in other programming languages.
Example 1: A Simple Function to Calculate Area
Let’s create a function that calculates the area of a rectangle. We’ll take the length and width as input and return the calculated area.
def calculate_rectangle_area(length, width):
"""
Calculates the area of a rectangle.
Args:
length: The length of the rectangle.
width: The width of the rectangle.
Returns:
The area of the rectangle.
"""
area = length width
return area
# Example usage:
rectangle_length = 5
rectangle_width = 10
area = calculate_rectangle_area(rectangle_length, rectangle_width)
print(f"The area of the rectangle is: {area}")
Explanation:
- We define a function named `calculate_rectangle_area` that takes two arguments: `length` and `width`.
- Inside the function, we calculate the area by multiplying `length` and `width`, storing the result in the `area` variable.
- We return the calculated `area`.
- We then call the function with sample values for `length` and `width`, and print the returned area.
Key Points:
- `def` keyword is used to define a function.
- `:` marks the end of the function definition.
- Indentation is crucial in Python to define the code block within the function.
- The `return` statement specifies the value that the function will output.
Common Mistake: Forgetting the colon (:) after the function name. If you miss this, Python will raise a SyntaxError.
Correction: Add the colon (:) at the end of the `def` line.
Output:
The area of the rectangle is: 50
Example 2: Function with Default Arguments
Now let’s create a function that calculates the volume of a cube. We’ll use default arguments for the side length, making the function more flexible. If the user doesn’t provide a side length, it will default to 5.
def calculate_cube_volume(side_length=5):
"""
Calculates the volume of a cube.
Args:
side_length: The length of one side of the cube. Defaults to 5.
Returns:
The volume of the cube.
"""
volume = side_length 3
return volume
# Example usage:
volume1 = calculate_cube_volume()
print(f"The volume of the cube is: {volume1}")
volume2 = calculate_cube_volume(3)
print(f"The volume of the cube is: {volume2}")
Explanation:
- We define a function named `calculate_cube_volume` that takes one argument, `side_length`, with a default value of 5.
- Inside the function, we calculate the volume by cubing the `side_length`.
- We return the calculated `volume`.
- We call the function twice: once with no arguments (uses the default side length) and once with a specified side length.
Key Points:
- Default arguments are specified after the parameter name within the parentheses.
- When the function is called without an argument for that parameter, the default value is used.
Common Mistake: Trying to change the default argument value after the function has been defined. This will not work. You must assign the default value when you define the function.
Correction: The default argument is assigned within the function definition.
Output:
The volume of the cube is: 125
The volume of the cube is: 27
Example 3: Passing Arguments by Value (Call by Value)
In Python, arguments are passed to functions by value. This means that when you pass a variable to a function, a copy of the value of that variable is sent to the function. Changes made to the variable inside the function do not affect the original variable outside the function. Let’s demonstrate this with a simple function that modifies a local variable.
def modify_value(x):
"""
Modifies a local variable and returns it.
Args:
x: The value to be modified.
Returns:
The modified value of x.
"""
x = x + 10
return x
# Example usage:
original_value = 5
modified_value = modify_value(original_value)
print(f"Original value: {original_value}")
print(f"Modified value: {modified_value}")
Explanation:
- We define a function named `modify_value` that takes one argument, `x`.
- Inside the function, we add 10 to the value of `x` and assign the result back to `x`.
- We return the modified value of `x`.
- We call the function with a value of 5, store the returned value in `modified_value`, and print both the original and modified values.
Key Points:
- When `x` is passed to the function, a copy of the value 5 is passed.
- Inside the function, `x` is a local variable that is independent of `original_value`.
- Modifying `x` inside the function does not change the value of `original_value`.
Output:
Original value: 5
Modified value: 15



Leave a Reply