Basic Functions
A function is essentially a set of instructions that you can use over and over again to perform a specific task. For example, in your piece of code, if you had to give an introduction about your dogs every time to anyone who asked, it would be a hassle to type it out every time. Instead, you just call a function that would do it instead. So instead of writing out 10+ lines every time you'd simply be writing one, to call said function.
In programming terminology, a function is a named block of code that takes some input (the parameters), does some work using that input and then produces an output.
#function to call to introduce the dogs
def introduce_dogs():
print("Hi, I'm wifi. I am currently 3 and a half years old. I love tomatoes")
print("Hi, I'm Phantom. I am turning 2 this year. I run really fast")
print("Hi, we're Jimmy and Jelly. We're just 6 months old and love chasing after wifi")
print("Hi, could you tell me about your dogs?")
print("Sure!")
introduce_dogs()
Functions with Inputs
You could also provide your function with inputs. The function will work with these inputs to perform its task. Suppose you wish to calculate the area of a rectangle. Your function would need length and breadth.
# Define a function to calculate the area of a rectangle
def calculate_rectangle_area(width, height):
area = width * height
print("The area of the rectangle with width", width, "and height", height, "is:", area)
# Call the function with specific width and height values
width = 5
height = 8
calculate_rectangle_area(width, height)
Functions with Outputs
Everything being done so far is contained within the function block. The 'return' statement in functions sends a value back to the caller of the function. Once the value has been returned it can be used in the rest of the program.
The 'return' statement also terminates the function's execution and sends the specified value back to the caller. Modifying the above example to illustrate the use of the return statement.
# Define a function to calculate the area of a rectangle
def calculate_rectangle_area(width, height):
area = width * height
return area
# Call the function with specific width and height values
width = 5
height = 8
result = calculate_rectangle_area(width, height)
# Output the result
print("The area of the rectangle with width", width, "and height", height, "is:", result)
Multiple return values
Python functions can have multiple return values, and these values can be returned as a tuple. Tuples are similar to lists but are immutable.
# Function to calculate the sum and product of two numbers
def calculate_sum_and_product(a, b):
sum_result = a + b
product_result = a * b
return sum_result, product_result
# Calling the function and storing the returned values in variables
sum_result, product_result = calculate_sum_and_product(8, 5)
# Using the returned values
print("Sum:", sum_result)
print("Product:", product_result)
One can use multiple return statements in a function through conditional logic. It is useful if we want to perform certain checks, before running the function.
# Function to compare two numbers and return information
def compare_numbers(a, b):
if a > b:
return "First number is greater", a
elif b > a:
return "Second number is greater", b
else:
return "Numbers are equal", a # We can return either number, they are equal
# Calling the function and storing the returned values in variables
result1, value1 = compare_numbers(8, 5)
result2, value2 = compare_numbers(4, 7)
result3, value3 = compare_numbers(6, 6)
# Using the returned values
print(result1, ":", value1)
print(result2, ":", value2)
print(result3, ":", value3)
Docstrings
Docstrings are a special type of comments used to provide documentation for functions, modules, classes or methods. Docstrings help developers, even yourself, understand the purpose and the usage behind the code you have written.
def my_function(param1, param2):
"""
This is a docstring for my_function.
This function takes two parameters, param1 and param2, and performs some operation.
You can provide more detailed information about what the function does, any
assumptions it makes, expected inputs, and the type of output it produces.
:param param1: Description of the first parameter.
:type param1: The expected type of param1.
:param param2: Description of the second parameter.
:type param2: The expected type of param2.
:return: Description of the return value (if any).
:rtype: The expected type of the return value.
"""
# Function implementation here
pass