Function in python programming | Hands on coding

 

Function in python

  • def keyword is used to define function in python.

Syntax of function

def fun_name(parameter list):
  function_body  
def
  • It is a keyword which is used to define function.
fun_name
  • It is the actual name of the function.
  • It is also used at the time of calling the function.
Parameter List
  • It is the place where we can pass a number of parameter/variable.
  • These variables may be used in the program.
  • The value of parameter is passed from the calling of function.
  • It is optional part.
Body
  • It is the place where the actual code is written to perform the specific task.
Example

#Function definition
def add():
    x=10
    y=30;
    z=x+y
    print("Add=",z)
#function calling
add()
"""
***Output***
Add=40
"""
  • Here add is function name.
Function with parameter

#Function definition
def fruits(name):
    print("I like ",name)
#function calling
fruits("Mango")
fruits("Orange")
fruits("Apple")
"""
***Output***
I like  Mango
I like  Orange
I like  Apple
"""

#Function definition
def fruits(name,fruit):
    print(name ," likes ",fruit)
#function calling
fruits("Anjali","Mango")
fruits("Nisha","Orange")
fruits("Rocky","Apple")
"""
***Output***
Anjali  likes  Mango
Nisha  likes  Orange
Rocky  likes  Apple
"""
Function with default parameter value
  • We can also assign a default value to a parameter.
  • If the function is called without argument, it uses the default value.

#Function definition
def fruits(name="Rocky",fruit="Apple"):
    print(name ," likes ",fruit)
#function calling without argument
fruits()
#function calling with argument
fruits("Nisha","Orange")
"""
***Output***
Rocky  likes  Apple
Nisha  likes  Orange
"""
Function with return type
  • We can also return a value from the function using return type.
  • return keyword is used to return a value.

#Function definition
def add(x,y):
    return (x+y)
#function calling
print("5+6=",add(5,6))
print("10+16=",add(10,16))
"""
***Output***
5+6= 11
10+16= 30
"""
Passing List as an Argument
  • We can also pass a list as an argument.

#Function definition
def fruits(fruitlist):
    for name in fruitlist:
     print("I like ",name)
mylist=["Mango","Orange","Apple"]
#function calling
fruits(mylist)
"""
***Output***
I like  Mango
I like  Orange
I like  Apple
"""
Arbitrary Arguments *args
  • If the number of arguments is unknown add asterisk(*) symbol before the parameter name.
  • We can access the arguments using index value.

#Function definition
def fruits(*fruitlist):
    print("I like ", fruitlist[0])
    print("I like ", fruitlist[1])
    print("I like ", fruitlist[2])
#function calling
fruits("Mango","Orange","Apple")
"""
***Output***
I like  Mango
I like  Orange
I like  Apple
"""
Function with pass statement
  • It is compulsory to define a function in python means we can not declare a function.
  • So to define a function with no content pass statement is used.

#function definition with no content
#using pass statement
def fruits(name):
    pass
#Function definition
def fruits(name):
    print("I like ", name)
#function calling
fruits("Mango")
"""
***Output***
I like  Mango
"""
Recursiont
  • The process of calling a function by itself is called recursion.
  • The benefit of recursion is that we can use it as a loop.

#Function definition
def myFunction():
    print("Hello")
    #function is calling by itself
    myFunction()
#function calling
myFunction()
"""
***Output***
Infinite time Hello
"""
Table of 5 using recursion

#Function definition
def table(no):
    if no<=50:
     print(no,end=" ")
     no=no+5
     #function is calling by itself
     table(no)
#function calling
table(5)
"""
***Output***
5 10 15 16 20 30 35 40 45 50 
"""
Factorial of any number using recursion

#Function definition
def factorial(no):
    if no>1:
     return  no*factorial(no-1)
    else:
     return 1
#taking user input
n=int(input("Enter any number:"))
#function calling
print("Factorial of ",n, " is ",factorial(n))
"""
***Output***
Enter any number:5
Factorial of  5  is  116
"""