You are on page 1of 22

Functions

Part-2
Function Arguments
Python supports the following types of formal arguments −
• Required arguments
• Keyword arguments
• Default arguments
• Variable-length arguments
Required Arguments
• Required arguments are the arguments passed to a function in
correct positional order.
• The number of arguments in the function call should match
exactly with the function definition.

def student_rec(name,roll,age):
print("Name of student is", name)
print("Roll of student is", roll)
print("Age of student is",age)

student_rec("AAA","17CCC001",21)
With different no of arguments
def student_rec(name,roll,age):
print("Name of student is", name)
print("Roll of student is", roll)
print("Age of student is",age)

student_rec("AAA","17CCC001")
Keyword Arguments
• Keyword arguments are related to the function calls. When
you use keyword arguments in a function call, the caller
identifies the arguments by the parameter name.
• This allows you to skip arguments or place them out of order
because the Python interpreter is able to use the keywords
provided to match the values with parameters.
def student_rec(name,roll,age):
print("Name of student is", name)
print("Roll of student is", roll)
print("Age of student is",age)

student_rec(name="AAA", age=21,roll="17CCC001")
Default Arguments
• A default argument is an argument that assumes a default
value if a value is not provided in the function call for that
argument.
def student_rec(name,roll,age=21):
print("Name of student is", name)
print("Roll of student is", roll)
print("Age of student is",age)

student_rec("AAA","17CCC001")
student_rec("BBB","17CCC002",22)
def student_rec(name,roll="17CCC001",age):
print("Name of student is", name)
print("Roll of student is", roll)
print("Age of student is",age)

student_rec("AAA",21)
student_rec("BBB","17CCC002",22)
Variable Length Arguments
• There may be a need to process a function for more arguments than
you specified while defining the function.
• These arguments are called variable-length arguments and are not
named in the function definition.
• Syntax
def functionname([formal_args,] *var_args_tuple ):
#function statements
return [expression]

• An asterisk (*) is placed before the variable name that holds the
values of all non keyword variable arguments.
• This tuple remains empty if no additional arguments are specified
during the function call.
Example: passing tuple
ef prtup(*name):
print(name)

prtup('sss','aaa','bbb')

def additem(*item):
item=item +(9,)
print("new item is",item)

additem(1,2,3,4,5,6)
Example
def student_rec(name,*courses):
print("Name of student is", name)
print(name + " is Registered with Following Courses")
for i in courses:
print(i)

student_rec("AAA","FOCP","Physics","Maths")
student_rec("BBB","FOCP","English")
Write a Python program to remove an
item from a tuple
def rmtup(*tup):
tup=tup[:2]+tup[3:]
return tup
#tup=tuple(lt)

print(rmtup(1,2,3,4,5,6))
print all unique values in a dictionary

def uniquedic(**dic):
print(set(dic.values()))

uniquedic(a=1,b=2,c=3,d=1,e=4,f=2)
Convert decimal to binary
n=int(input('enter a number'))
num=n; binary=0;base=1
while n>0:
remainder=n%2
binary=binary+remainder*base
n=n//2
base=base*10
print(base)

print("Its binary euivalent",binary)


Using functions
def binary(n):
binary=0;base=1
while n>0:
remainder=n%2
binary=binary+remainder*base
n=n//2
base=base*10
print("Its binary euivalent",binary)

n=int(input('enter a number'))
binary(n)
Local vs. Global Scope
def sum(n1,n2):
res=n1+n2
return res

result=sum(5,4)
print(res)
AA mistake!
mistake! What
What
happens
happens here?
here?
Local variables
def c_to_f(c):
result = c / 5.0 * 9 + 32 Local scope
return result

tempf = c_to_f(19) Global scope


print result

English definition:
scope (n): extent or range of view, outlook, application, operation, effectiveness, etc.:
• Local variable: variable that is assigned a value inside a
function
• Belongs to the function in which it was created
• Only statements inside that function can access it, error will occur if
another function tries to access the variable
• Scope: the part of a program in which a variable may be
accessed
• For local variable: function in which created
Global Variables
• Global variable: created by assignment statement written
outside all the functions
• Can be accessed by any statement in the program file, including
from within a function
• If a function needs to assign a value to the global variable, the
global variable must be redeclared within the function
• General format: global variable_name
Local Scope and Global Scope revisited
• When Python encounters a variable, it
• first checks to see if the variable is defined in the local scope
• then checks to see if the variable is defined in the next most
outer scope
• then checks to see if the variable is defined in the next most
outer scope
• …
• then checks to see if the variable is defined in the global scope
Nesting of functions
def fun1():
print("inside first function")
fun2()
print("returned first function")

def fun2():
print("called second function")

fun1()
Cont’d.
def fun1():
print("inside first function")
fun2()
print("returned to first function")

def fun2():
print("called second function")
fun3()
print("returned to second function")

def fun3():
print("called third function")

fun1()
Pascal’s Triangle
import math
def pascal(n):
for i in range(0,n):
for k in range(0,i+1):

term=math.factorial(i)//(math.factorial(k)*math.factorial(i-k))
print(term,end=' ')
print("\n")

n=int(input("How many rows you want to show in pascal


triangle?"))
pascal(n)

You might also like