Python Basics: Functions
Tags: python, devops, basic
Published: 2024-05-11 15:17:19

Functions in Python serve the dual purpose of enhancing code readability and promoting reusability. Each function encapsulates specific logic, facilitating easier debugging processes.

Below is a basic Python program demonstrating the use of functions:

```
# Declaring Global Variables
a = 50
b = 20

def add():               # Defining a function named 'add'
    add_result = a + b   # Defining logic for addition
    print(add_result)    # Printing the result of addition

def sub():                # Defining a function named 'sub'
    sub_result = a - b    # Defining logic for subtraction
    print(sub_result)     # Printing the result of subtraction

def mul():                # Defining a function named 'mul'
    mul_result = a * b    # Defining logic for multiplication
    print(mul_result)     # Printing the result of multiplication

```
If we execute the above code, no output will be printed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqm1qw9w47al0g6qp124.png)
====

In this Python script, to execute the defined functions, we need to call them explicitly.

###Declaring Global Variables

```
# Declaring Global Variables
a = 50
b = 20

def add():               # Define function named 'add'
    result = a + b      # Define logic
    print(result)       # Output the result

def sub():               # Define function named 'sub'
    result = a - b       # Define logic
    print(result)        # Output the result

def mul():               # Define function named 'mul'
    result = a * b       # Define logic
    print(result)        # Output the result

add()  # Call add function
sub()  # Call sub function
mul()  # Call mul function

```

When we run this script, it will explicitly call each function (add(), sub(), mul()) in sequence, producing the respective outputs.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnnm0ywdv7dnah31s3s8.png)


When we want to assign specific values to each function instead of relying on global variables, we can define parameters for each function.

```
def add(a, b):               # Define function named 'add' with parameters 'a' and 'b'
    result = a + b           # Define logic
    return result            # Return the output

def sub(a, b):               # Define function named 'sub' with parameters 'a' and 'b'
    result = a - b           # Define logic
    return result            # Return the output

def mul(a, b):               # Define function named 'mul' with parameters 'a' and 'b'
    result = a * b           # Define logic
    return result            # Return the output

print(add(5, 6))              # Call add function with values 5 and 6 and print the result
print(sub(18, 7))             # Call sub function with values 18 and 7 and print the result
print(mul(5, 8))              # Call mul function with values 5 and 8 and print the result

```
In this script, each function (add(), sub(), mul()) accepts two parameters (a and b) representing the values to be operated on. When we call each function with specific values, it returns the respective outputs.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqxg87ou9piw1mj343g6.png)