You are on page 1of 2

Functions_Own

Ibrahim Abou-Faycal

#
EECE-231
#
Introduction to Computation & Programming with Applications
#
Functions - Own 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]

0.1 Designing own functions


Why?
• Code decomposition
• Code reuse
• Abstraction: hiding implementation details, which are not needed by someone using the
function as a black box
• Divide code into manageable blocks
• Team work

0.1.1 Syntax

[ ]: def functionName (formalParameter1, formalParameter2, formalParameter3):


# body of function
pass

• If the function returns a value, the function body contains a return statement
return value

1
• Otherwise, its body may or may not contain a return statement to stop the execution of the
function
return
• Calling the function:
[ ]: actualParameter1 = 1
actualParameter2 = 3
actualParameter3 = 2
functionName(actualParameter1, actualParameter2, actualParameter3)

Example
[ ]: def maxVal(x,y):
"""
Return the max of x and y
"""
if x > y:
return x
else:
return y

# Calling the function


v = maxVal(13,7)
print(v)
# Or simply:
#print(maxVal(3,7))
help(maxVal)

You might also like