1. FUNCTION IN PYTHON
CLASS: XII
COMPUTER SCIENCE -083
USER DEFINED FUNCTIONS
PART-4
FUNCTIONS
with
PARAMETERS
2. Syntax to define User Defined Functions:
def Function_name (parameters or arguments):
[statements……]
…………………………….
…………………………….
Its keyword and
used to define the
function in python
Statements inside the def begin after four
spaces, this is called Indentation.
These parameters
3. Example To define User Defined Functions:
def myname(nm):
print(nm)
This is function definition
To call this function in a program
myname(‘WELCOME’) This is function calling
----Output-----
WELCOME
Full python code:
def myname(nm):
print(nm)
myname(‘WELCOME’)That nm is a parameter
4. Display the 4 names “JAMES”, “ Blake”, “Tom”, ”John” with the help
of one function and one print only.
def display1():
print(“JAMES”)
def display4():
print(“John”)
def display3():
print(“Tom”)
def display2():
print(“Blake”)
display1()
display2()
display3()
display4()
---Output-----
JAMES
John
Tom
Blake
5. So what problem we face , that is four name four functions
with different function name .Then we call all these
functions.
What we need here that one function name that use to
display all four names.
6. Display the 4 names “JAMES”, “ Blake”, “Tom”, ”John” with the help
of one function and one print only.
def display(nm):
display(‘JAMES’)
---Output-----
JAMES
John
Tom
Blake
print(nm)
display(‘Blake’)
display(‘Tom’)
display(‘John’)
We passes nm as a
parameter that store the
values passes from
display() at the bottom.
7. So WHAT is important part in parameterized function it is
the parameter pass inside the function header, and what is
the work of this parameter is to store the value pass from
the calling function.
Now again we take one more example to understand
parameterized functions.
8. In this we pass a number 5 as a parameter through a function name add()
and display the sum of 5 with itself.
def sum(n):
print(“sum=“,n+n)
sum(5)
When we execute the program the function sum() will call and
value 5 passes to a parameter no.
This is the original value and it is called actual parameter. Actual
parameters are values that are passed to a function when it is
invoked.Here 5 is a actual parameter
This is copied value for the function. So the function
sum() uses this value that comes from actual parameter
and this value known as formal parameter. Formal
parameters are the variables defined by the function
that receives values when the function is called. So
here n is a formal parameter
9. So now if we need to add to numbers means we need to pass two numbers
as a parameter and display there sum. Function name sum() with two
parameters.
def sum(a,b):
print(“sum=“,a+b)
sum(5,6)
a, b are formal parameter and store the copied value
passes through actual parameter sum(5,6)
----Output----
sum=11
10. Program to accept the two numbers from user and display there addition
through a function sum() with two parameters .
def sum(a,b):
print(“sum=“,a+b)
sum(x,y)
a, b are formal parameter and store the copied
value passes through actual parameter sum(x,y)
Where x value passes to a and y value to b
----Output----
Enter the value x: 7
Enter the value y: 3
sum=10
x=int(input(“Enter the value x:”))
y=int(input(“Enter the value y:”))
11. Program to accept the radius from user and display area of circle using
function circle() with radius as a parameter.
def circle(r):
print(“Area of circle=“,3.14*r*r)
circle(rad)
r is formal parameter and store the copied value
passes through actual parameter circle(rad)
Where rad value passes to r, so rad is actual
parameter and r is a formal parameter.
----Output----
Enter radius: 3
Area of circle=28.259
rad=int(input(“Enter radius:”))
12. Program to accept the 5 subject marks and display total and percentage.
Function name studentmrk().
def studentmrk(m1,m2,m3,m4,m5):
total=m1+m2+m3+m4+m5
per=total/5
print(“total:”,total, “per:”,per)
studentmrk(mk1,mk2,mk3,mk4,mk5)
m1,m2,m3,m4,m5 are formal parameter and
store the copied value passes through actual
parameter
studentmrk(mk1,mk2,mk3,mk4,mk5)
----Output----
Enter marks1 : 35
Enter marks2 : 45
Enter marks3 : 55
Enter marks4 : 65
Enter marks5 : 75
total:275 per: 55.0
mk1=int(input(“Enter marks1:”))
mk2=int(input(“Enter marks2:”))
mk3=int(input(“Enter marks3:”))
mk4=int(input(“Enter marks4:”))
mk5=int(input(“Enter marks5:”))
13. Program to accept the number and check it’s a even number or odd number
using function name checkevod() with 1 parameter
def checkevod(n):
if( n% 2== 0):
print(“Its even”)
else:
print(“its odd”)
checkevod(no)
n is formal parameter and store the copied value
passes through actual parameter checkevod(no)
----Output----
Enter number: 3
Its oddno=int(input(“Enter number:”))
14. Program to define a function checkage() with age as a parameter accept the
age from the user and check its age is valid to vote if age is more than equal
to 18 otherwise print “you a not valid to vote”
Program to define a function checkPN() with n variable as parameter and
check that number pass through parameter is positive or negative.
Program to define a function checkleapyr() with year as a parameter and
check it’s a leap year or not.
15. How to use nested if’s inside the function.
For example: Program define function name dis_day() with 1 parameter and accept no(1-
7) and print 1 for “Monday”,2 for “Tuesday” ………
def dis_day(n):
if( n== 1):
print(“Monday”)
elif(n== 2):
print(“Tuesday”)
elif(n== 3):
print(“Wedneday”)
elif(n== 4):
print(“Thursday”)
elif(n== 5):
print(“Friday”)
elif(n== 6):
print(“Saturday”)
elif(n== 7):
print(“Sunday”)
else:
print(“Invalid”)
----Output----
Enter days(1-7): 3
Wednesday
dis_day(n)
no=int(input(“Enter days (1-7):”))
16. So now if we need to design calculator with the parameterized function and nested if’s.
For example: Program define function name calculate() with 3 parameters and accept two
operands and one operator (+,-,*,/,//) from the user and display the output.
def calculate(x,y,op):
if( op== ‘+’):
print(x,”+”,y,”=“,x+y)
elif(op== ‘-’):
print(x,”-”,y,”=“,x-y)
elif(op== ‘*’):
print(x,”*”,y,”=“,x*y)
elif(op== ‘/’):
print(x,”/”,y,”=“,x/y)
elif(op== ‘//’):
print(x,”//”,y,”=“,x//y)
----Output----
Enter value1: 3
Enter value2: 4
Enter operator(+,-,*,/,//): *: +
3 + 4 =7
calculate(n1,n2,opp)
n1=int(input(“Enter value1:”))
n2=int(input(“Enter value2:”))
opp=int(input(“Enter operator(+,-,*,/,//:”))