PYTHON
PYTHON
PYTHON
Functions
Introduction to Functions
Defining and Calling a Void Function
Designing a Program to Use Functions
Local Variables
Passing Arguments to Functions
Global Variables and Global Constants
Topics (cont’d.)
Introduction to Value-Returning
Functions: Generating Random
Numbers
Writing Your Own Value-Returning
Functions
The math Module
Storing Functions in Modules
Introduction to Functions
Function: group of statements within a
program that perform as specific task
Usually one task of a large program
Functions can be executed in order to perform
overall program task
Known as divide and conquer approach
Modularized program: program wherein
each task within the program is in its
own function
Benefits of Modularizing a
Program with Functions
The benefits of using functions include:
Simpler code
Code reuse
write the code once and call it multiple times
Better testing and debugging
Can test and debug each function individually
Faster development
Easier facilitation of teamwork
Different team members can write different
functions
Void Functions and Value-
Returning Functions
A void function:
Simply executes the statements it contains and then
terminates.
A value-returning function:
Executes the statements it contains, and then it
returns a value back to the statement that called it.
The input, int, and float functions are
examples of value-returning functions.
Defining and Calling a Function
def my_function():
print('And now for')
print('something completely')
print('different.')
Designing a Program to Use
Functions
In a flowchart, function call shown as
rectangle with vertical bars at each side
Function name written in the symbol
Typically draw separate flow chart for each
function in the program
End terminal symbol usually reads Return
Top-down design: technique for
breaking algorithm into functions
Designing a Program to Use
Functions (cont’d.)
Hierarchy chart: depicts relationship
between functions
AKA structure chart
Box for each function in the program, Lines
connecting boxes illustrate the functions called by
each function
Does not show steps taken inside a function
Use input function to have program wait for
user to press enter
Designing a Program to Use
Functions (cont’d.)
Local Variables
Local variable: variable that is assigned
a value inside a function
Belongs to the function in which it was
created
Only statements inside that function can access it,
error will occur if another function tries to access
the variable
Scope: the part of a program in which a
variable may be accessed
For local variable: function in which created
Local Variables (cont’d.)
Local variable cannot be accessed by
statements inside its function which
precede its creation
Different functions may have local
variables with the same name
Each function does not see the other
function’s local variables, so no confusion
Passing Arguments to
Functions
Bookex5_4.py
Argument: piece of
data that is sent into a def main():
value = 5
function show_double(value)
Bookex5_6.py
# This program displays a random number
# in the range of 1 through 10.
import random
def main():
# Get a random number.
number = random.randint(1, 10)
# Display the number.
print('The number is', number)