Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Variables, Expression and...
  5. Parameters and Arguments

Parameters and Arguments

Parameters

  • A parameter is the variable listed inside the parenthesis in the function definition
  • If there is more than one value, then all are separated by comma

Arguments

  • An argument is the value that is sent to the function when it is called
  • Bounding of parameters to arguments is done 1:1 and so there should be same number and type of arguments as mentioned in the parameter list

Example

def my_add(a,b):
    z = a + b
    return z
x= int(input("Enter first number"))
y= int(input("Enter second number"))
c = my_add(x,y)
print("Result of addition =",c)

In this example, a, b are parameters and x,y are arguments. The value of arguments x, y are assigned to the parameters a, b.

Different types of Function arguments

The following are the types of formal arguments

  • Required arguments
  • Keyword arguments
  • Default arguments
  • Variable length arguments (Arbitrary arguments)
  • Arbitrary keyword 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 printdata(name, sub, mark):
    print("Name =", name)
    print("Subject =", sub)
    print("Mark = ",mark)
print("Students Details")
printdata("Raji","Python",95)
Students Details
('Name =', 'Raji')
('Subject =', 'Python')
('Mark = ', 95)

Keyword arguments

  • Keyword arguments are send as key = value
  • The order of the arguments does not matter in keyword arguments
  • Rule: A non keyword argument after keyword arguments is not allowed.

Example

printdata(“def”, sub=”English”, 100) leads to error

def printdata(name, sub, mark):
    print("Name =", name)
    print("Subject =", sub)
    print("Mark = ",mark)
print("Students Details")
printdata(name="Raji", sub = "Python", mark=100)
printdata(sub = "physics", name = "ABC", mark = 98)
printdata(mark= 95, name="xyz", sub = "chemistry")
printdata("uvw","Maths",mark = 100)

Output

Students Details
('Name =', 'Raji')
('Subject =', 'Python')
('Mark = ', 100)
('Name =', 'ABC')
('Subject =', 'physics')
('Mark = ', 98)
('Name =', 'xyz')
('Subject =', 'chemistry')
('Mark = ', 95)
('Name =', 'uvw')
('Subject =', 'Maths')
('Mark = ', 100)

Default arguments

  • If a value is not provided in the function call for a particular argument, then the default value specified in the function parameter is assumed for that argument
  • Default arguments should be assigned from left to right i.e. without while calling a function default value will be assigned from left to right
def printdata(name="abc", mark=98):
    print("Name=", name)
    print("mark =",mark)
printdata("Raji") # passing only one argument
printdata("xyz",100) #passing both arguments

Output

('Name=', 'Raji')
('mark =', 98)
('Name=', 'xyz')
('mark =', 100)

Variable length arguments

  • If the number of arguments passed to a function is unknown, then a * is added before the parameter name in the function definition
  • In this way, the function will receive a tuple of arguments and can access the items accordingly
def printdata(*args):
    print("Subjects are", args)
    print("Subject  in args[0] ", args[0])
    print("Subject  in args[1] ", args[1])
    print("Subject  in args[2]",args[2])

printdata("python","chemistry", "physics")

Output

('Subjects are', ('python', 'chemistry', 'physics'))
('Subject  in args[0] ', 'python')
('Subject  in args[1] ', 'chemistry')
('Subject  in args[2]', 'physics')

Arbitrary keyword arguments

  • If the number of keyword arguments to pass to the function is not known, then add two asterisk * before the parameter name in the function definition
  • This way the function will receive the dictionary of arguments
def myfun(**Fullname):
    print("Full name is "+Fullname['fname']+Fullname['lname'])
myfun(fname="John", lname="Tomas")

Output

Full name is JohnTomas

Loading

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments