Funct-Arg
Tue 09 December 2025
# Default Arguments in Python
def studentInfo(name, gender = 'Male'):
Cell In[2], line 1
def studentInfo(name, gender = 'Male'):
^
SyntaxError: incomplete input
def studentInfo(name, gender = 'Male'):
# This function displays the student's info passed in the function parameters.
print('Name:',name)
print('Gender:',gender)
# Main program.
# Function call 1.
studentInfo('Deepak')
Name: Deepak
Gender: Male
# Function call 2.
studentInfo('Tripti', gender = 'Female')
Name: Tripti
Gender: Female
def studentInfo(name, rollNo = 20, branch = 'Electrical'):
print('Name:',name,'Roll no:',rollNo,'Branch:',branch)
# Main program.
# Function call 1.
studentInfo(name = 'John')
Name: John Roll no: 20 Branch: Electrical
# Function call 2.
studentInfo(name = 'Bob', rollNo = 10)
Name: Bob Roll no: 10 Branch: Electrical
# Function call 3.
studentInfo(name = 'Jenny', rollNo = 5, branch = 'Computer Science')
Name: Jenny Roll no: 5 Branch: Computer Science
# Required Arguments in Python
# example error code
# Program to find the area and perimeter of rectangle using function required arguments.
def rectangle(length, breadth):
areaRec = length * breadth # Area of rectangle.
perRec = 2 * (length + breadth) # Perimeter of rectangle.
print("Area of rectangle = ",areaRec)
print("Perimeter of rectangle = ",perRec)
# Main program.
def main():
ln = int(input("Enter the length of rectangle: "))
br = int(input("Enter the breadth of rectangle: "))
rectangle(ln) # Intentionaly missing one argument value.
main() # function calling.
Enter the length of rectangle: 43
Enter the breadth of rectangle: 43
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[23], line 1
----> 1 main() # function calling.
Cell In[22], line 4, in main()
2 ln = int(input("Enter the length of rectangle: "))
3 br = int(input("Enter the breadth of rectangle: "))
----> 4 rectangle(ln)
TypeError: rectangle() missing 1 required positional argument: 'breadth'
# Keyword Arguments in Python
# Displaying persons info.
def display(name, age, gender):
print('Name:',name)
print('Age:',age)
print('Gender:',gender)
def main():
# First function call.
display('John', 20, 'M') # function calling from another function.
# Second function call.
display('Jenny', 18, 'F')
main() # function calling.
Name: John
Age: 20
Gender: M
Name: Jenny
Age: 18
Gender: F
# Displaying persons info.
def display(name, age, gender):
print('Name:',name)
print('Age:',age)
print('Gender:',gender)
def main():
# First function call.
display('Herry', age = 20, gender = 'M') # function calling from another function.
# Second function call.
display(age = 18, gender = 'F', name = 'Jimmy')
main() # function calling.
Name: Herry
Age: 20
Gender: M
Name: Jimmy
Age: 18
Gender: F
# Variable length Arguments
# args is the name of variable length parameters.
def my_function(*args):
"function docstring"
# body of the function
return [expression]
# Example 1:
# Function definition with variable length parameters.
def my_function(*args): # Here, args is name of formal parameter.
print(args)
print('Calling function with two arguments')
my_function(10, 20)
print('Calling function with three arguments')
my_function(10, 20, 30)
print('Calling function with four arguments')
my_function(20, 30, 40, 50)
Calling function with two arguments
(10, 20)
Calling function with three arguments
(10, 20, 30)
Calling function with four arguments
(20, 30, 40, 50)
# Function definition with one formal parameter and variable length parameters.
def listDay(arg1, *arg2):
print(arg1, arg2, sep="")
def main():
listDay('Name of days are:', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
main() # function calling.
Name of days are:('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
def listDay(arg1, *arg2):
print(arg1, arg2, sep="")
def main():
listDay('Name of days are:', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
main() # function calling.
Name of days are:('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
Score: 40
Category: Python basics