PYTHON FUNCTION
PYTHON FUNCTIONS
SYNTAX OF USER DEFINED FUNCTION :
def function_name(parameters):
"""docstring"""
statement(s)
HOW FUNCTION WORKS IN PYTHON ?
def functionname():
. . . . . . . .
. .
. . . . . .
.
. . . . . . .
.
. . . . . . .
functionname();
.
. . . . . . .
.
. . . . . . .
EXAMPLE
def welcome_func(name):
"""this function
welcomes to
the person passed in as
parameter """
printf("hello
," +name +".good morning !")
FUNCTION CALL
>>> welcome_func('ram')
hello , ram . good morning !
EXAMPLE
#1
program to find the square value of a number using UDF function
#function to find the square value of a number
def sample(a) :
c=a*a
return
(c)
#function call
n=int(input("enter a
number:"))
res=sample(n)
print("the square value of %d is
%d "%(n,res))
OUTPUT
enter a number : 5
the square value of 5 is 25
SCOPE AND LIFETIME OF
VARIABLES
def my_func();
x=10
print("value
inside function :",x)
x=20
my_func()
print("value outside function :
",x)
OUTPUT
value inside fulnction : 10
value outside function : 20
PYTHON FUNCTION ARGUMENTS
VARIABLE FUNCTION ARGUMENTS
PYTHON DEFAULT ARGUMENTS
def welcome_func(name , msg =
"good morning !"):
"""
this function
welcomes to
the person with the
provided message .
if messege is not
provided,
it defaults to
"good morning!"
"""
print
("hello",name + ','+msg)
welcome_func("kumerasan")
welcome_func("prakash",
"have a nice day")
PYTHON KEYWORD ARGUMENTS
#example for keyword argument
def welcome_func(name,msg):
print("hai",name,msg)
name="karthick"
welcome_func(name="karthick",msg="have
a nice day")
welcome_func(msg = "have a nice
day",name="karthick")
welcome_func(name,msg="have a nice
day")
OUTPUT
hai karthick have a nice day
hai karthick have a nice day
hai karthick have a nice day
PYTHON ARBITARY ARGUEMENTS
def arb_func(*names):
"""this function
welcomes all
the person in the names
tuple."""
#names is a tuple with arguements
for na in names :
print("hello",na)
arb_func("priyadharshini","radhika","samantha","jheevikha")
OUTPUT
hello priyadharshini
hello radhika
hello samantha
hello jheevikha
PYTHON RECURSION
PYTHON RECURSIVE FUNCTION
Function call itself is called Recursive Function
#an example of a recursive function to
#find the factorial of a number
def calc_factorial(x):
"""this is a recursive
function
to find the factorial of an integer
"""
if x==1:
return 1
else:
return (x*calc_factorial(x-1))
num=4
print("the factorial of
",num,"is",calc_factorial(num))
PYTHON ANONYMOUS / LAMBDA FUNCTIONS
LAMBDA FUNCTIONS
Anonymous Function are also called Lambda Function.
Lambda functions can have any number of argument but only one
Expression.
EXAMPLE
OF LAMBDA FUNCTIONS
#program to show the use of lambda functions
double = lambda x:x * 2
#output: 10
print(double(5))
f= lambda x,y:x+y
print(f(5,1))
is nearly the same as
def sumtwo(x,y):
return(x+y)
print(sumtwo(5,1))
#example for arithemetic operation of two number
In python we can use already saved files by using import module filename
#saved the file in the name of jksample.py
def jkaddition(x,y):
a=x+y
return(a)
def jksubtraction(x,y):
a=x-y
return(a)
def jkmultiplication(x,y):
a=x*y
return(a)
def jkdivision(x,y):
a=x/y
return(a)
how to call from >>> prompt
call from prompt
>>>import
jksample
>>>print jkmultiplication(3,4)
12
>>>print jkaddition(100,200)
300
>>>print jkdivision(20,2)
10
>>>print jksubtraction(30,2)
28
>>>
0 comments:
Post a Comment