You are on page 1of 23

METU Computer Engineering

CEng 240 – Spring 2021


Week 8
Sinan Kalkan

Functions [continued]

Disclaimer: Figures without reference are from either from “Introduction to programming concepts
with case studies in Python” or “Programming with Python for Engineers”, which are both co-authored
by me.
0!
Functions in Python
4
N G2
CE
on
METU Computer Engineering

sly
u
vio
e
Pr

¢ Syntax is important!
¢ Indentation is extremely important!

2020 S. Kalkan & G. Ucoluk - CEng 111 2


G2
4Nested Functions in Python
0!
N
CE
on
METU Computer Engineering

sly
u
vio
e
Pr

2020 S. Kalkan & G. Ucoluk - CEng 111 3


0!
Global Variables in Python
4
N G2
CE
on
METU Computer Engineering

sly
u
io
ev
Pr ¢ To access variables in the global workspace,
you should use “global <varname>”

2020 S. Kalkan & G. Ucoluk - CEng 111 4


0!
Scope in Python
4
N G2
CE
o n
METU Computer Engineering

ls y
u
vio
e
Pr ¢ Since you can nest
functions in Python,
understanding scope
is important

¢ LEGB rule:
Local < Enclosing <
Global < Built-in
2020 S. Kalkan & G. Ucoluk - CEng 111 5
Parameter 40! passing in functions in
2

on
CENG Python
METU Computer Engineering

sly
u
vio
e
Pr

2020 S. Kalkan & G. Ucoluk - CEng 111 6


Parameter 40! passing in functions in
2

on
CENG Python
METU Computer Engineering

sly
u
vio
e
Pr

2020 S. Kalkan & G. Ucoluk - CEng 111 7


Parameter 40! passing in functions in
2

on
CENG Python
METU Computer Engineering

sly
u
vio
e
Pr

2020 S. Kalkan & G. Ucoluk - CEng 111 8


0!
Default Parameters in Python
4
N G2
CE
o n
METU Computer Engineering

ls y
u
vio
e
Pr

¢ We can now call this function with reverse_num()


in which case Number is assumed to be 123.
¢ If we supply a value for Number, that value is
used instead.

2020 S. Kalkan & G. Ucoluk - CEng 111 9


This Week
METU Computer Engineering

¢ Previous week was:


§ Why define functions?
§ Defining functions
§ Parameter passing
§ Default parameters
§ Scope of variables
§ Examples
¢ This week:
§ Recursion, higher-order functions
§ Examples
2021 S. Kalkan - CEng 240 10
Administrative Notes
METU Computer Engineering

¢ Lab 5
¢ Midterm: 1 June, Tuesday, 17:40

2021 S. Kalkan - CEng 240 11


Higher-order functions
METU Computer Engineering

¢ map(function, Iterator)

¢ filter(predicate, Iterator)

2021 S. Kalkan - CEng 240 12


Recursion: An action wizard
METU Computer Engineering

¢ What is recursion?

2020 S. Kalkan & G. Ucoluk - CEng 111 13


Recursion: An example
METU Computer Engineering

¢ Definition of factorial:

• More formally:

2020 S. Kalkan & G. Ucoluk - CEng 111 14


Recursion: an example (cont’d)
METU Computer Engineering

¢ A careful look at the formal definition:

Factorial uses
its own definition!
2020 S. Kalkan & G. Ucoluk - CEng 111 15
Recurrence & Recursion
METU Computer Engineering

¢ This is called recurrence relation/rule.


¢ Algorithms which make use of recurrence
relations are called recursive.

2020 S. Kalkan & G. Ucoluk - CEng 111 16


Recursion: an example (cont’d)
METU Computer Engineering

¢ Let us look at the pseudo-code:

def factorial(N):
if N == 0: return 1
else: return N * factorial(N-1)

2020 S. Kalkan & G. Ucoluk - CEng 111 17


Recursion: another example
METU Computer Engineering

¢ Recursive definition of even or odd (a


mutually recursive example):

2020 S. Kalkan & G. Ucoluk - CEng 111 18


Another example for recursion
METU Computer Engineering

¢ Reversing a list / string


§ [a, b, c] à [c, b, a]
¢ reverse([a, b, c]) = reverse([b, c]) + [a].

2020 S. Kalkan & G. Ucoluk - CEng 111 19


Function Examples
METU Computer Engineering

What is printed by the following code piece?

N = 1
M = 2

def f():
global M
N = 3
M = 4
print(N, M)

f()
print(N,M)

2021 S. Kalkan - CEng 240 20


Function Examples
METU Computer Engineering

¢ Finding the median


§ https://pp4e-workbook.github.io/chapters/functions/finding_median.html

¢ Bankruptcy Countdown:
§ https://pp4e-workbook.github.io/chapters/functions/bankruptcy_countdown.html

¢ Perfect Number:
§ https://pp4e-workbook.github.io/chapters/functions/perfect_number.html

2021 S. Kalkan - CEng 240 21


Final Words:
Important Concepts
METU Computer Engineering

¢ Benefits of defining functions


¢ How to define functions
¢ Default parameters
¢ Scopes of variables

2021 S. Kalkan - CEng 240 22


METU Computer Engineering

THAT’S ALL FOLKS!


STAY HEALTHY

2021 S. Kalkan - CEng 240 23

You might also like