Functions in Python
Defining
The Functions:
body of function
def
keyword is used to define the functions in Python. function_name is user
defined name followed by pair of parenthesis and colon(:). Then write body of
function.
for example,
def
samplefunction():
print("This is fiunction
definition")
print("Simple function")
In
above example def keyword is used to define samplefunction(). It is user
defined name. In the above program no any output will display because function is not
called.
Function Call:
To
call a function use function name followed by parenthesis.
Syntax:
function-name()
For
Example:
def
samplefunction():
print("This is fiunction
definition")
print("Simple function")
samplefunction()
O/P:
This is fiunction definition
Simple function
Function
Return:
return
keyword is used to return the values to function. When the function is called
the values are returned to function.
Example
to compare two numbers and display result
def
simplenumbers(x,y):
if x>y:
return x
else:
return y
simplenumbers(10,5)
O/P:
10
#
Modify the result
def
simplenumbers(x,y):
if x>y:
return x
else:
return y
result=simplenumbers(10,55)
print("The
biggest number",result)
O/P:
The biggest number 55
x=int(input("Enter
first number:\n"))
y=int(input("Enter
second number:\n"))
def
comparenumbers(x,y):
if x>y:
return x
else:
return y
result=comparenumbers(x,y)
print("The
biggest number is:\n",result)
O/P:
Enter first number
20
Enter second number
10
The biggest number is:
20
Arguments in Functions:
Argument is nothing but the
values that are passed within the parenthesis of function.
We can use one or more than
one arguments written in pair of parenthesis of function and separated them
with comma.
Following function is
defined with one argument.
# function with one
argument
def myname(name):
print("My name is",name)
myname("Manisha")
O/P:
My name is Manisha
def
myname(fname,mname,lname):
print("My name
is",fname,mname,lname)
myname("Manisha","M","More")
O/P:
My name is Manisha M More
def addition(a,b,c):
print("Addition of three numbers
is:\t",a+b+c)
addition(10,20,30)
O/P:
Addition of three numbers
is: 60
a=int(input("Enter
first number\n"))
b=int(input("Enter
second number\n"))
c=int(input("Enter
third number\n"))
def addition(a,b,c):
print("Addition of three numbers
is:\t",a+b+c)
addition(a,b,c)
O/P:
Enter first number
20
Enter second number
20
Enter third number
20
Addition of three numbers is: 60
0 टिप्पण्या
कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏