Python: Functions Functions Mathematical functions f(x) = x 2 - - PowerPoint PPT Presentation

python functions functions
SMART_READER_LITE
LIVE PREVIEW

Python: Functions Functions Mathematical functions f(x) = x 2 - - PowerPoint PPT Presentation

Python: Functions Functions Mathematical functions f(x) = x 2 f(x,y) = x 2 + y 2 In programming functions also help creating better structure with decomposition Functions


slide-1
SLIDE 1

Python: Functions

slide-2
SLIDE 2

Functions

Mathematical functions f(x) = x2 f(x,y) = x2 + y2 In programming functions also help creating better structure with decomposition

slide-3
SLIDE 3

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/

slide-4
SLIDE 4

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/

slide-5
SLIDE 5

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/

slide-6
SLIDE 6

Defining and invoking a function

Consider f(x) = x2 def square(x):

#defining function

return x*x

square(4) #invoking function 16 # output

slide-7
SLIDE 7

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

slide-8
SLIDE 8

Defining and invoking a function

Example: Function calling another function def repeatmyprint():

myprint() myprint() repeatmyprint() #invoking function Hello world # output Hello world

slide-9
SLIDE 9

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/

slide-10
SLIDE 10

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/

slide-11
SLIDE 11

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/

returns 4

slide-12
SLIDE 12

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/

slide-13
SLIDE 13

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/

No argument One argument One argument (function) ( ) ( ) ( )

slide-14
SLIDE 14

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/

( ) ( ) ( ) ( ) ( ) ( )

slide-15
SLIDE 15

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/

( ) ( ) ( ) ( ) ( ) ( )

slide-16
SLIDE 16

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/

( ) ( ) ( ) ( ) ( ) ( )

slide-17
SLIDE 17

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/

Output

inside func_a None ( ) ( ) ( ) ( ) ( ) ( )

slide-18
SLIDE 18

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/

Output

inside func_b 7 ( ) ( ) ( ) ( ) ( ) ( )

slide-19
SLIDE 19

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/

Output

inside func_c inside func_a None ( ) ( ) ( ) ( ) ( ) ( )

slide-20
SLIDE 20

Function: Scope

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/

x is redefined locally

2 5

Output

slide-21
SLIDE 21

Function: Scope

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/

Can access x defined outside Output

5 6 5

slide-22
SLIDE 22

Function: Scope

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/

Can not modify x defined outside UnboundLocalError Output

slide-23
SLIDE 23

Function: Scope (Example)

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/

Output

g:x=4 4 print (z)