DEV Community

bharatrajtj
bharatrajtj

Posted on

Python Basics: Functions

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

Enter fullscreen mode Exit fullscreen mode

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

Image description

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

Enter fullscreen mode Exit fullscreen mode

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

Image description

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

Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)