Python - Functions

In Python, a function is a block of organized, reusable code that performs a specific task. It takes some input arguments, performs operations on them, and returns the output.

The basic syntax for creating a function in Python is as follow

def function_name(parameters):
    # function body
    return value

Here, function_name is the name of the function, parameters are the input arguments to the function, and value is the output returned by the function.

Let's take an example of a function that adds two numbers:

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

Here, the function add_numbers takes two input arguments a and b, adds them together, and returns the result.

To call the function, we simply pass in the required input arguments:

sum = add_numbers(2, 3)
print(sum)

In Python, when we pass a variable to a function, it can either be passed by reference or by value, depending on the data type of the variable.

If the variable is an immutable data type like integers, strings, or tuples, it is passed by value. This means that a copy of the variable is made and passed to the function, so any changes made to the variable inside the function do not affect the original variable outside the function.

If the variable is a mutable data type like lists or dictionaries, it is passed by reference. This means that the function receives a reference to the original variable, and any changes made to the variable inside the function are reflected in the original variable outside the function.

Here's an example to illustrate the difference:

def change_value(x):
    x = 2
def change_list(lst):
    lst.append(4)
# passing integer by value
x = 1
change_value(x)
print(x)  # output: 1
# passing list by reference
lst = [1, 2, 3]
change_list(lst)
print(lst)  # output: [1, 2, 3, 4]

In the above example, change_value does not modify the original variable x because x is an integer and is passed by value. On the other hand, change_list modifies the original list lst because lst is a mutable list and is passed by reference.

Default arguments are a way to specify a default value for a function parameter, so that if the function is called without providing a value for that parameter, the default value is used.

Here's an example:

def greet(name, greeting='Hello'):
    print(greeting, name)
greet('Alice')  # Output: Hello Alice
greet('Bob', 'Hi')  # Output: Hi Bob

In this example, the greet function has a parameter called greeting which has a default value of 'Hello'. When we call greet('Alice'), since we didn't provide a value for greeting, the default value is used and the function outputs "Hello Alice". When we call greet('Bob', 'Hi'), we provide a value for greeting, so the default value is ignored and the function outputs "Hi Bob".

The scope of a variable refers to the region of the program where the variable is defined and can be accessed. There are two types of variable scopes in Python:

Local Scope: Variables defined within a function have a local scope. They can only be accessed within the function and not outside it.

def my_function():
    x = 10
    print("Value of x inside the function:", x)
my_function()
#Output: Value of x inside the function: 10

In this example, the variable x is defined within the function my_function() and can only be accessed within it.

Global Scope: Variables defined outside a function have a global scope. They can be accessed from anywhere in the program, including within a function.

x = 10
def my_function():
    print("Value of x inside the function:", x)
my_function()
print("Value of x outside the function:", x)
#Output:
#Value of x inside the function: 10
#Value of x outside the function: 10

In this example, the variable x is defined outside the function my_function() and can be accessed both inside and outside the function.

Note that if a variable with the same name is defined both inside and outside a function, the local variable takes precedence over the global variable within the function. To access the global variable within the function, the global keyword can be used.