Function Programs
P1: Multiplication Of two nos. using function
defmul():
x=4
y=5
Mul=x*y
print("Multiplication is: ",Mul)
return
mul()
O/P:
Multiplication is: 20
**************************************************************************************
def greet(name, msg):
"""This function greets to
the person with the provided message"""
print("Hello", name + ', ' + msg)
greet("Monica", "Good morning!")
Output
Hello Monica, Good morning!
Here, the function greet() has two parameters.
Since we have called this function with two arguments, it runs smoothly and we
do not get any error.
If we call it with a different number of arguments, the interpreter will show an
error message. Below is a call to this function with one and no arguments along
with their respective error messages.
>>>greet("Monica") # only one argument
TypeError: greet() missing 1 required positional argument: 'msg'
>>>greet() # no arguments
TypeError: greet() missing 2 required positional arguments: 'name' and
'msg'
P3:Arguments:
defmy_function(fname):
print(fname + " References")
my_function("Email")
my_function("Tobias")
my_function("Linus")
O/P
Email References
Tobias References
Linus References
***************************************************************************************
P4:#input from user subtraction
def sub(a,b):
s=a-b
return s
n1=int(input("Enter a:"))
n2=int(input("Enter b:"))
s=sub(n1,n2)
print("subtraction is: ",s)
*******************************************************************************************
# parameter& arguments in python
defmul(x,y):
m=x*y
print("Multiplication is: ",m)
return m
mul(10,3)
******************************************************************************************
Default Arguments
defgreet(name, msg="Good morning!"):
"""
This function greets to
the person with the
provided message.
If the message is not provided,
it defaults to "Good
morning!"
"""
print("Hello", name + ', ' + msg)
greet("Kate")
greet("Bruce", "How do you do?")
Output
Hello Kate, Good morning!
Hello Bruce, How do you do?
*******************************************************************************************
Scope of variable:
x = 300
def myfunc():
x = 200
print(x)
myfunc()
print(x)
**********************************************************************************
Area of triangle in function
# Area of a Triangle using Functions
import math
Def Area_of_Triangle(a, b, c):
# calculate the Perimeter
Perimeter = a + b + c
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
Area = math.sqrt((s*(s-a)*(s-b)*(s-c)))
print("\n The Perimeter of Traiangle = %.2f" %Perimeter);
print(" The Semi Perimeter of Traiangle = %.2f" %s);
print(" The Area of a Triangle is %0.2f" %Area)
Area_of_Triangle(6, 7, 8)
O/P:
The Perimeter of Traiangle = 21.00
The Semi Perimeter of Traiangle = 10.50
The Area of a Triangle is 20.33
>>>Area_of_Triangle(10, 9, 12)
The Perimeter of Traiangle = 31.00
The Semi Perimeter of Traiangle = 15.50
The Area of a Triangle is 44.04
>>>
*****************************************************************************************
#Even or odd
num=int(input("Enter a number for check odd or even: "))
def find_Evenodd(num):
if(num%2==0):
print(num," Is an even")
else:
print(num," is an odd")
find_Evenodd(num);//function call
*******************************************************************************
Area of circle:
Def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
num=float(input("Enter r value:"))
print("Area is : " % findArea(num));