Professional Documents
Culture Documents
Ibrahim Abou-Faycal
#
EECE-231
#
Introduction to Computation & Programming with Applications
#
Functions - Handling Other Functions
Reading: [Guttag, Sections 4.1,4.2, 4.5, and 5.3] Material in these slides is based on
• Slides of EECE-230C & EECE-230 MSFEA, AUB
• [Guttag, Chapter 4]
• [MIT OpenCourseWare, 6.0001, Lecture 4, Fall 2016]
print(f(1))
# We can't call g() from outside f !
print(g(2))
1
0.1.2 Function returning another function
Key concept: in Python, functions are objects
[ ]: def f(b):
if b==1:
def g(y):
return y
else:
def g(y):
return y*y
return g
print( f(1)(10) )
[ ]: print(f(2)(25))
def findSum(n,p):
x = 0
for i in range(1,n+1):
x = x + p(i)
return x
print(findSum(10, linear))
[ ]: print(findSum(10, square))