Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Programming using Python

Amey Karkare
Dept. of CSE
IIT Kanpur

For any queries reach out to rahulgr@iitk.ac.in or rahulgarg@robustresults.com


or whatsapp 9910043510

1
Python Programming
Keyword and Default Arguments
Keyword Arguments
def printName(first, last, initials) : Note use of [0] to get the
first character of a string.
if initials: More on this later.
print (first[0] + '. ' + last[0] + '.')
else:
print (first, last)
Call Output
printName('Acads', 'Institute', False) Acads Institute
printName('Acads', 'Institute', True) A. I.
printName(last='Institute', initials=False, first='Acads') Acads Institute
printName('Acads', initials=True, last='Institute') A. I.

3
Nov-16 Programming, Functions
Keyword Arguments
• Parameter passing where formal is bound to
actual using formal's name
• Can mix keyword and non-keyword arguments
– All non-keyword arguments precede keyword
arguments in the call
– Non-keyword arguments are matched by position
(order is important)
– Order of keyword arguments is not important

4
Nov-16 Programming, Functions
Default Values
def printName(first, last, initials=False) :
if initials:
print (first[0] + '. ' + last[0] + '.')
else: Note the use of
“default” value
print (first, last)
Call Output
printName('Acads', 'Institute') Acads Institute
printName(first='Acads', last='Institute', initials=True) A. I.
printName(last='Institute', first='Acads') Acads Institute
printName('Acads', last='Institute') Acads Institute

5
Nov-16 Programming, Functions
Default Values
• Allows user to call a function with fewer
arguments
• Useful when some argument has a fixed value
for most of the calls
• All arguments with default values must be at
the end of argument list
– non-default argument can not follow default
argument

6
Nov-16 Programming, Functions
Returning from a function: return statement

• When a return statement is encountered in a


function definition
– control is immediately transferred back to the
statement making the function call in the parent
function.
• A function returns only ONE value or NONE.
– If nothing is returned, the return type is NoneType
with value as None

7
Nov-16 Programming, Functions

You might also like