Python - Functions

13.
Discuss the use of global variables within a function.

In Python, a global variable is a variable declared outside of any function or block of code and is accessible throughout the entire program. When a global variable is used within a function, it can be read without any special declaration. However, if you want to modify the global variable from within a function, you need to use the global keyword.

Let's illustrate this with an example:

# Global variable
global_var = 10

def modify_global():
    # Access global variable
    print("Global variable inside function:", global_var)

    # Modify global variable using the 'global' keyword
    global global_var
    global_var += 5

# Call the function
modify_global()

# Print the modified global variable
print("Modified global variable outside function:", global_var)

Output:

Global variable inside function: 10
Modified global variable outside function: 15

In this example, the function modify_global can access and modify the global variable global_var using the global keyword. The changes made inside the function are reflected outside the function as well.


14.
How can you document a function in Python?

In Python, you can document a function using docstrings, which are string literals that occur as the first statement in a module, class, method, or function definition. They are used to provide documentation and can be accessed using the __doc__ attribute.

Let's create a simple example with a documented function:

def calculate_area(length, width):
    """
    Calculate the area of a rectangle.

    Parameters:
    - length (float): The length of the rectangle.
    - width (float): The width of the rectangle.

    Returns:
    float: The area of the rectangle.
    """
    area = length * width
    return area

# Access the docstring using the __doc__ attribute
print(calculate_area.__doc__)

# Call the function
rectangle_area = calculate_area(5, 8)
print("Area of the rectangle:", rectangle_area)

Output:

Calculate the area of a rectangle.

    Parameters:
    - length (float): The length of the rectangle.
    - width (float): The width of the rectangle.

    Returns:
    float: The area of the rectangle.

Area of the rectangle: 40

In this example, the calculate_area function is documented using a docstring that describes its purpose, parameters, and return value. The docstring can be accessed using the __doc__ attribute.


15.
Explain the purpose of the pass statement in a function block.

The pass statement in a function block in Python is a no-operation statement. It serves as a placeholder when syntactically some code is required but no action is desired or necessary. It is often used when defining a function or a conditional block where the body of the function or the block is empty.

Let's illustrate the purpose of the pass statement with an example:

def placeholder_function():
    pass

# Call the function
placeholder_function()

# The function does nothing, and it doesn't produce any output

In this example, the placeholder_function is defined using the pass statement. When the function is called, it doesn't perform any specific action, and there is no output. The pass statement allows the function to be syntactically correct without having any executable code.


16.
What is a closure in Python, and how can you create one?

In Python, a closure is a function object that has access to variables in its lexical scope, even when the function is called outside that scope. It allows a function to remember and access the variables in the environment where it was created, even if the function is executed in a different scope.

Here's an example illustrating the concept of a closure:

def outer_function(x):
    # Inner function is defined within the scope of outer_function
    def inner_function(y):
        return x + y
    # Return the inner function, creating a closure
    return inner_function

# Create closures with different values of x
closure_1 = outer_function(10)
closure_2 = outer_function(5)

# Call the closures with different values of y
result_1 = closure_1(3)
result_2 = closure_2(3)

# Print the results
print(result_1)  # Output: 13
print(result_2)  # Output: 8

In this example, outer_function returns inner_function as a closure. Each closure has access to its own lexical scope's variable x, and calling the closures with different values of y produces different results. The closures "remember" the value of x from their creation environment.