training day 5
Functions in Python
Functions in python consists of two parts : Calling and definition. Here def keyword is used to define the function .
Example :
def one( ): #definition
print("Hello")
one( ) #calling
Example :
def one(last):
print(last + "Kaur")
one("Jaspreet")
one("Parneet")
Parameter with default value :
Example :
def one(first = ''Jaspreet'')
print(first + "kaur")
one("Puneet")
one()
Parameter passing in the form of list
Example :
def check_fruits(fruits):
for x in fruits:
print(x)
fruits = ["apple","banana'',"cherries"]
check_fruits(fruits)
Returning
Example :
def multiply(n):
return 5*n
print(multiply(2))
print(multiply(3))
print(multiply(4))
Recursion
Program for factorial of number using recursion
n = int(input("Enter any number"))
def factorial(n):
if n == 0:
return 1;
else:
return n*(factorial(factorial(n-1))
print(factorial(n))
Comments
Post a Comment