Functions in Python

 

Python Functions

Syntax :
                keyword def    function_name () :

Function is a collection of statement(s), to perform certain operation(s).
Re-usability of code is the main advantage of function.
As informed earlier, there is no { } concept, instead we : (colon) and then followed by indentation.

Function can be defined in 4 ways : 
  • Without parameter, Without return

#program to a function defined as without parameter and without return
def add() :
a =
5
b = 10
c = a+b
print(c)

add()

#output
15


  • Without parameter, With return

#program to a function defined as without parameter and with return
def add() :
a =
5
b = 10
return a+b

c = add()
print(c)

#output
15


  • With parameter, Without return

#program to a function defined as with parameter and without return
def add(a,b) :
c = a+b
print(c)

 a = 5
b = 10
add(a,b)

#output
15


  • With parameter, With return

#program to a function defined as with parameter and with return
def add(a,b) :
return a+b


 a = 5
b = 10
c = add(a,b)
print(c)
#output
15








Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India