Unit 3 (First)
Unit 3 (First)
• Functions
• A function is a block of code that performs a specific task. Dividing a
complex problem into smaller chunks makes our program easy to
understand and reuse.
• A function runs only when it is called.
• They are defined using the def keyword
• They can be called multiple times.
• Types of function(2 types)
• Standard library functions - These are built-in functions in Python
that are available to use.
• User-defined functions - We can create our own functions based on
our requirements.
• Function Declaration
• syntax
– def function_name(arguments):
– # function body
– return
• def add(x,y):
• z=x+y
• return z
• a=10
• b=20
• add(a,b)
• #print(result)
• #print ("a = {} b = {} a+b = {}".format(a, b,
result))
• # via input box
• def add():
• n=int(input("Enter the FNo"))
• b=int(input("Enter the SNo"))
• print(n+b)
• add()
• Arguments : An argument is a value that is
accepted by a function.
Information can be passed into functions
as arguments . Arguments are specified after
the function name, inside the parentheses.
You can add as many arguments as you want.
4 types of arguments that can provide in a
function :
1. Default Arguments
2. Keyword Arguments
3. Variable length Arguments
4. Required Arguments or positional
• #passing argument / parameter
• def add(y):
– X=10
– C=x+y
• print(c)
• add(50)
• def get_square(num):
• return num * num
• for i in [1,2,3,4,7,9]:
• # function call
• result = get_square(i)
• print('Square of',i, '=',result)
• # argument and return
• def add(x,y):
• return(x+y)
• z=add(10,20)
• print(z)
• Note :
– A parameter is the variable listed inside the
parentheses in the function definition.
– An argument is the value that is sent to the
function when it is called.
• #factorial of any number
• def fact(x):
• i=1
• f=1
• while x>=i:
• f=f*i
• i=i+1
• print(f)
• fact(5)
Types of Actual Arguments
• 4 types :
– Positional Arguments
– Keyword Arguments
– Default Arguments
– Variable Length Arguments
• Positional Arguments
– These arguments are passed to the function in correct
positional order.
– The number of arguments and their positions in the
function definition should be equal to the number and
position of the argument in the function call.
• # Positional Arguments or actual arguments
• def pw(x,y):
• z=x**y
• print(z)
• pw(5,3)
• Default Argument :
– Default values indicate that the function argument will take
that value if no argument value is passed during function
call. The default value is assigned by using assignment (=)
operator.
• Eg :
– def defa(name,location="Janakpuri"):
– print(f"name:{name} location: {location}")
– defa("MSI")
• Variable Length Arguments, *args
– Variable length argument is an argument that can accept any number of
values. The variable length argument is written with * symbol.
– It stores all value in a tuple.
• Eg :the user doesn’t know in advance how many no of arguments
will pass, them the *args will help it.
– Def calcval(*args):
– # Print(args)
– totalval=sum(args)
– return(totalval)
– ## call the fun
– Calcval(10,30,40,80,90,100,45)
• Eg : # using slicing
– def fun(*kids):
– print("The youngest child is ", kids[1:3])
– fun("hello","I","love","Python")
• Eg : # show only index value
– def fun(*kids):
– print("The programming is ", kids[3])
– fun("hello","I","love","Python")
• Eg :
– # show all values
– def fun(*kids):
– print("The programming is ", kids)
– fun("hello","I","love","Python")
Keyword Argument
– numbers = (1, 2, 3, 4)
– result = map(calculateSquare, numbers)
– print(result)
• def fact(num):
• if num==1:
• return num
• return num*fact(num-1)
• number=int(input("Enter a number : "))
• result=fact(number)
• print("factorail of ",number," is ",result)