Python: Functions
Functions
Mathematical functions
f(x) = x2
f(x,y) = x2 + y2
In programming functions also help creating better
structure with decomposition
Functions
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Defining and invoking a function
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Defining and invoking a function
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Defining and invoking a function
Consider f(x) = x2
def square(x): #defining function
return x*x
square(4) #invoking function
16 # output
Defining and invoking a function
Example: Functions may not have arguments, and
return statement
def myprint(): #defining function
print (“Hello world”)
myprint() #invoking function
Hello world # output
Defining and invoking a function
Example: Function calling another function
def repeatmyprint():
myprint()
myprint()
repeatmyprint() #invoking function
Hello world # output
Hello world
Scope of a Variable
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Scope of a Variable
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Scope of a Variable
returns 4
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Scope of a Variable
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Arguments
No argument
( )
One argument
( )
One argument
(function)
( )
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Arguments
( )
( )
( )
( )
( )
( )
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Arguments
( )
( )
( )
( )
( )
( )
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Arguments
( )
( )
( )
( )
( )
( )
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Arguments
( )
( )
( ) Output
( ) inside func_a
( ) None
( )
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Arguments
( )
( )
( ) Output
( )
inside func_b
( )
7
( )
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Arguments
( )
( )
( ) Output
( )
( ) inside func_c
( ) inside func_a
None
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Scope
x is redefined locally
Output
2
5
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Scope
Can access x
defined outside
Output
5
6
5
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Scope
Can not modify
x defined outside
Output
UnboundLocalError
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Function: Scope (Example)
Output
g:x=4
print (z) 4
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/