Functions

def

Python def keyword is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function.In Python, a function is a logical unit of code containing a sequence of statements indented under a name given using the “def” keyword. In Python def keyword is the most used keyword.

Syntax

def function_name(parameters):

Simple function

# defining function
def func():
    print("Hello")

# calling function    
func()

Function with parmeters and return

# function for subtraction of 2 numbers.
def sub(x, y):
    z = x-y
    return(z)

# main code
a = 90
b = 50

# finding subtraction
s = sub(a, b)

# print statement
print("subtraction of",a,"and",b,"is",s)

Practise Programs

  1. Create a simple interest function and use the function to find simple interest
  2. Create three functions add, subtract and multiply, take 2 numbers and use add , sbutract and multiply function
  3. Take a list of principal values with time = 5 and interest_rate = 5. then use simple interest function on all principal values.