Skip to main content

Command Palette

Search for a command to run...

Day 21 - Function Arguments and Statements

Updated
4 min read
Day 21 - Function Arguments and Statements
C
I am Data Science student, has a little bit knowledge on Web Development. I also love writing and editing as my hobby. Passionate to explore the world.

There are four types of arguments that we can provide in a function:

  • Default Arguments

  • Keyword Arguments

  • Variable length Arguments

  • Required Arguments

1. Default arguments:

We can provide a default value while creating a function. This way the function assumes a default value even if a value is not provided in the function call for that argument.

Example 1 :

def average(a=9, b=1): #default arguments
    print("The average is ", (a+b)/2)
average()

Output:

The average is  5.0

\=========================================================

Example 2 :

def average(a=9, b=1): #default arguments
    print("The average is ", (a+b)/2)
average(1,5) #it ignores above values and take a=1 and b=5

Output:

The average is  3.0

\=========================================================

Example 3 :

def average(a=9, b=1): #default arguments
    print("The average is ", (a+b)/2)
average(5) #here only value of a is given so a=5 and b=1 (i.e. deafult value given above)

Output:

The average is  3.0

\=========================================================

Example 4 :

def average(a=9, b=1): #default arguments
    print("The average is ", (a+b)/2)
average(b=9) #here only value of b is given so b=9 and a=9 (i.e. deafult value given above)

Output:

The average is  9.0

\=========================================================

Example 5 :

def name(fname, mname = "Jhon", lname = "Whatson"):
    print("Hello,", fname, mname, lname)

name("Amy")

Output:

Hello, Amy Jhon Whatson

2. Keyword arguments:

We can provide arguments with key = value, this way the interpreter recognizes the arguments by the parameter name. Hence, the the order in which the arguments are passed does not matter.

Example 1 :

def average(a=9, b=1): #keyword arguments
    print("The average is ", (a+b)/2)
average(b=9, a=21) #here order of value doesnot matters

Output:

The average is  15.0

\=========================================================

Example 2 :

def name(fname, mname, lname):
    print("Hello,", fname, mname, lname)

name(mname = "Peter", lname = "Wesker", fname = "Jade")

Output:

Hello, Jade Peter Wesker

3. Required arguments:

In case we don’t pass the arguments with a key = value syntax, then it is necessary to pass the arguments in the correct positional order and the number of arguments passed should match with actual function definition.

Example 1:

def average(a, b, c=1): #required arguments
    print("The average is ", (a + b + c) / 2)
average(5,6) 

#In above function value of a and b is not given, so we have given there values here a=5, b=6 and c=1 (default value given above)

Output:

The average is  6.0

\=========================================================

Example 2:

def average(a, b): #required arguments
    print("The average is ", (a+b)/2)
average(4, 6)

Output:

The average is  5.0

\=========================================================

Example 3:

def name(fname, mname, lname):
    print("Hello,", fname, mname, lname)
name("Peter", "Ego", "Quill")

Output:

Hello, Peter Ego Quill

4. Variable-length arguments:

Sometimes we may need to pass more arguments than those defined in the actual function. This can be done using variable-length arguments.

There are two ways to achieve this:

  1. Arbitrary Arguments

  2. Keyword Arbitrary Arguments

Arbitrary Arguments:

While creating a function, pass a * before the parameter name while defining the function. The function accesses the arguments by processing them in the form of tuple.

Example 01 :

def average(*numbers): 
    #print(type(number)) # tuple - it takes input as tuple/list.
    sum = 0 #initialize
    for i in numbers:
        sum = sum + i
        print("Average is: ", sum/len(numbers))

#average(5,6)  #output: Average is:  5.5
average(5, 6, 7, 1) #output: Average is:  4.5

#with the help of above function, we can take average of as many numbers as we want

Output:

Average is:  4.5

\======================================================== Example 02 :

def name(*name):
    print("Hello,", name[0], name[1], name[2])

name("James", "Buchanan", "Barnes")

Output:

Hello, James Buchanan Barnes

Keyword Arbitrary Arguments:

While creating a function, pass a * before the parameter name while defining the function. The function accesses the arguments by processing them in the form of dictionary.

Example 01 :

def name(**name):
    #print(type(number)) # dictionary - it takes input as dictionary.
    print("Hello,", name["fname"], name["mname"], name["lname"])

name(mname = "Buchanan", lname = "Barnes", fname = "James")

Output:

Hello, James Buchanan Barnes

return Statement

The return statement is used to return the value of the expression back to the calling function.

Example 01 :

def average(*numbers): 
    sum = 0 #initialize
    for i in numbers:
        sum = sum + i
        return sum/len(numbers)

c = average(5, 6, 7, 1)
print(c)   #return function stores value and save it in varaible

Output:

4.75

\======================================================== Example 02 :

def average(*numbers): 
    sum = 0 #initialize
    for i in numbers:
        sum = sum + i
        return 7    #If we use use more thane one return statement, then jo bhi pehley return mileyga, yeh return statement ussy return kareygi.
        return sum/len(numbers)

c = average(5, 6, 7, 1)
print(c)   #return function stores value and save it in varaible

Output:

7

\======================================================== Example 03 :

def name(fname, mname, lname):
    return "Hello, " + fname + " " + mname + " " + lname

print(name("James", "Buchanan", "Barnes"))

Output:

Hello, James Buchanan Barnes

100DaysofPython

Part 22 of 35

This series is for beginners in which we explore python language along with how it is used in data science and do some exercises and some python related projects.

Up next

Day 22 - Introduction to Lists

Python Lists Lists are ordered collection of data items. They store multiple items in a single variable. List items are separated by commas and enclosed within square brackets []. Lists are change