Function in python
- def keyword is used to define function in python.
Syntax of function
def fun_name(parameter list): function_body
- It is a keyword which is used to define function.
- It is the actual name of the function.
- It is also used at the time of calling the function.
- 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.
- It is the place where the actual code is written to perform the specific task.
#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 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 with default parameter value#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 """
- We can also assign a default value to a parameter.
- If the function is called without argument, it uses the default value.
Function with return type#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 """
- We can also return a value from the function using return type.
- return keyword is used to return a value.
Passing List as an Argument#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 """
- We can also pass a list as an argument.
Arbitrary Arguments *args#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 """
- If the number of arguments is unknown add asterisk(*) symbol before the parameter name.
- We can access the arguments using index value.
Function with pass statement#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 """
- 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.
Recursiont#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 """
- The process of calling a function by itself is called recursion.
- The benefit of recursion is that we can use it as a loop.
Table of 5 using recursion#Function definition def myFunction(): print("Hello") #function is calling by itself myFunction() #function calling myFunction() """ ***Output*** Infinite time Hello """
Factorial of any number 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 """
#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
"""