0% found this document useful (0 votes)
15 views

Functions Own

The document discusses designing own functions in Python. It notes that functions allow for code decomposition, reuse, abstraction by hiding implementation details, and dividing code into manageable blocks. It provides the syntax for defining a function, including using def to specify the function name and parameters, and optionally returning a value. An example function maxVal is given that returns the maximum of two values passed in and demonstrates calling and printing the result of a function.

Uploaded by

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

Functions Own

The document discusses designing own functions in Python. It notes that functions allow for code decomposition, reuse, abstraction by hiding implementation details, and dividing code into manageable blocks. It provides the syntax for defining a function, including using def to specify the function name and parameters, and optionally returning a value. An example function maxVal is given that returns the maximum of two values passed in and demonstrates calling and printing the result of a function.

Uploaded by

Salman Masri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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