Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

5 Python Functions

The document discusses functions in Python. It provides examples of defining and invoking functions with and without arguments. It explains the scope of variables in functions, how local variables can redefine outer variables, and how outer variables can be accessed but not modified within functions. Functions can call other functions and return values.

Uploaded by

Mahboob Riaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

5 Python Functions

The document discusses functions in Python. It provides examples of defining and invoking functions with and without arguments. It explains the scope of variables in functions, how local variables can redefine outer variables, and how outer variables can be accessed but not modified within functions. Functions can call other functions and return values.

Uploaded by

Mahboob Riaz
Copyright
© © All Rights Reserved
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