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

Programming using Python

prutor.ai 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
Parts of a function

prutor.ai
Input

Output 2
Programming, Functions
def max (a, b):
‘’’return maximum among a and b’’’
keyword if (a > b):
return a 2 arguments
else: a and b

prutor.ai
Function Name return b (formal args)

Body of thefunction,
x = max(6, 4) indented w.r.t the
def keyword
Documentation comment
Call to the function. (docstring), type
Actual args are 6 and 4. help <function-name>
on prompt to get help for
3
Programming, Functionsthe function
def max (a, b):
‘‘‘return maximum among a and b’’’
if (a > b):
return a
else:

prutor.ai
return b

In[3] : help(max)
Help on function max in module __main__:

max(a, b)
return maximum among a and b
4
Programming, Functions
Function Call
• A function call is an expression
– feeds the necessary values to the function

prutor.ai
arguments,
– directs the function to perform its task, and
– receives the return value of the function.
• Similar to an operator application
5 + 3 is an expression
of type integer that
evaluates to 8 max(5, 3) is an expression
of type integer that
evaluates to 5 5
Programming, Functions
Function Call
• Since a function call is an expression
– it can be used anywhere an expression can be
used

prutor.ai
– subject to type restrictions
print (max(5,3)) prints 5
max(5,3) – min(5,3) evaluates to 2
max(x, max(y, z)) == z checks if z is max
of x, y, z
if (max(a, b) != 0): prints Y if max of a
print ('Y') and b is not 0.

6
Programming, Functions

You might also like