You are currently viewing Introduction to functions

Introduction to functions

Functions are blocks of code that perform specific tasks and can be reused throughout a program. They allow you to write code that is modular, easier to read and understand, and less prone to errors.

Here is an example of a simple function that takes two arguments and returns their sum:

def add_numbers(a, b):
    return a + b

In this example, the def keyword is used to define the function name (add_numbers), followed by the function parameters in parentheses (a and b). The body of the function is indented and contains a single line that returns the sum of the two arguments.

To use this function, you can simply call it with two arguments, like this:

result = add_numbers(5, 7)
print(result) # Output: 12

In this example, the add_numbers() function is called with two arguments (5 and 7). The result of the function call is stored in the result variable, which is then printed to the console.

Functions can be used for a wide range of tasks, from simple arithmetic to complex data manipulation. For example, you could write a function that reads a CSV file and returns a list of dictionaries, or a function that calculates the average temperature for a given location over a period of time.

The use of functions helps to break down complex tasks into smaller, more manageable pieces. It also allows for the reusability of code, making it easier to maintain and update.

I hope this example helps to illustrate the basics of functions in Python! Let me know if you have any questions.

def calculate_rectangle_area(length, width):
    area = length * width
    return area

In this example, the calculate_rectangle_area() function takes two arguments (length and width) and calculates the area of a rectangle using the formula area = length * width. The result is then returned using the return statement.

To use this function, you can call it with two arguments and store the result in a variable:

result = calculate_rectangle_area(5, 3)
print(result) # Output: 15

In this example, the function is called with the arguments 5 and 3, which represent the length and width of a rectangle. The result of the function call is stored in the result variable, which is then printed to the console.

Functions can also have optional arguments with default values. For example:

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

In this example, the greet() function takes a required argument (name) and an optional argument (greeting) with a default value of “Hello”. The function returns a string that includes the greeting and the name.

To use this function, you can call it with one or two arguments:

print(greet("Alice")) # Output: Hello, Alice!
print(greet("Bob", "Hi")) # Output: Hi, Bob!

In the first example, the function is called with only one argument (name), so the default greeting of “Hello” is used. In the second example, the function is called with two arguments (name and greeting), so the second argument is used instead of the default.

I hope these examples help to demonstrate the versatility and usefulness of functions in Python! Let me know if you have any questions.

def filter_even_numbers(numbers):
    result = []
    for num in numbers:
        if num % 2 == 0:
            result.append(num)
    return result

In this example, the filter_even_numbers() function takes a list of numbers as an argument and returns a new list containing only the even numbers from the original list.

The function uses a for loop to iterate over each number in the input list. It then uses an if statement to check if the number is even (i.e., divisible by 2 with no remainder). If the number is even, it is appended to the result list using the append() method.

To use this function, you can call it with a list of numbers and store the result in a variable:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = filter_even_numbers(numbers)
print(result) # Output: [2, 4, 6, 8, 10]

In this example, the function is called with the numbers list, which contains integers from 1 to 10. The result of the function call is a new list containing only the even numbers from the original list, which is then stored in the result variable and printed to the console.

I hope this example helps to illustrate how functions can be used to perform more complex operations on data structures like lists. Let me know if you have any further questions or if there’s anything else I can help with!

Leave a Reply