Functional Programming:
Python supports functional programming by developing
functions
Function:
It is a small block of code used to perfrom a logic
Features of functions:
By default functions is non executable.
It executes on calling or using the function in
main or home
Function offers reusability
Every function contains a)prototype\ signature
Function also contains logic\body
Signature- name, arguments, type of args,return values
Arguments are optional as well as return values are also optional
Types of funtion definition:
1. func with args with return values # add(a,b)
2. Func with args without return values #swap(x,y)
3. Func without args with return values# get code (.)
4. Func without args without return value # exit()
Implementation of functions
1. define function
(non executable code)
2. call/use the function
(executable sec)/main()
Function with args with return values
This definition is recommended to performs the
logic on argument as well as to return the values
#define funtion(non executable section)
Def add(a,b):
C=a+b
Return c
#executable section (call funtion)
R=add(17,19)
Print(R )
Print(add(10,90))
#dynamic values
X=int(input(“enter a value”)
Y=int(input(“enter b value”)
Print(add(x,y)
Define a sub function that return simple interest
def compute(p,t,r):
i=(p*t*r)/100
return i
p=1000
r=2
t=1
print(compute(p,t,r))
Define a sub function that return area of rectangle
Def area(l,b):
A=l*b
Return a
L=int(input(“enter l value”)
B=int(input(“enter b value”)
Area(l,b)
Print(area)
Function with arguments without return value
This function body definition does not contain
return value
Def greet(nm):
Print(“hello:”,nm)
Great(“krishnaveni”)
Define a function that prints area and circumference of square by taking sides as
arguments
Def sqrt(side):
A=side*side
C=4*side
Print(a)
Print(c)
Sqrt(45)
Define a function that draws a line with an argument which is passed as argument.
def drawline(ch):
for I in range(50):
print(ch, end=" ")
print()
drawline("*")
print("happy birthday")
drawline("*")
With return value without arguments
WAP to print fixed number
def get_number():
return 10
num = get_number()
print("Returned value:", num)
WAP to print sum of two numbers without arguments and with return value
def add():
a = 5
b = 7
return a + b
result = add()
print("Sum:", result)
WAP to return area of a circle
def area_circle():
r = 7
return 3.14 * r * r
area = area_circle()
print("Area of Circle:", area)
WAP to return the largest of two numbers
def largest():
a = 12
b = 20
if a > b:
return a
else:
return b
big = largest()
print("Largest number:", big)
Without return and without arguments
WAP to print greeting message
def message():
print("Welcome to Python programming")
message()
WAP to print sum of two numbers without return and without arguments
def add():
a = 10
b = 20
print("Sum:", a + b)
add()
Wap to check even or odd
def even_odd():
n = 15
if n % 2 == 0:
print("Even number")
else:
print("Odd number")
even_odd()
WAP to print 1 to 5
def print_numbers():
for i in range(1, 6):
print(i)
print_numbers()