0% found this document useful (0 votes)
9 views23 pages

Python Lecture 5 Python Functions

The document provides an overview of functions in Python, including their definition, invocation, and the concept of variable scope. It illustrates examples of functions with and without arguments, as well as function calls and the impact of variable scope on function behavior. Additionally, it highlights the importance of functions in programming for creating structured and decomposed code.

Uploaded by

Abhishek Goutam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views23 pages

Python Lecture 5 Python Functions

The document provides an overview of functions in Python, including their definition, invocation, and the concept of variable scope. It illustrates examples of functions with and without arguments, as well as function calls and the impact of variable scope on function behavior. Additionally, it highlights the importance of functions in programming for creating structured and decomposed code.

Uploaded by

Abhishek Goutam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

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/

You might also like