Functions
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
Introduction to Value-Returning
Functions: Generating Random
Numbers
Writing Your Own Value-Returning
Functions
The math Module
Storing Functions in Modules
• 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.
def step1():
pass
def step2():
pass
General format:
def function_name(parameter):
Figure 5-14 The value variable and the number parameter reference the same value
def change_me(arg):
print('I am changing the value.')
arg = 0
print(f'Now the value is {arg}.')
Figure 5-25 IPO charts for the getRegularPrice and discount functions
def get_name():
# Get the user’s name.
name = input(‘Enter your name:’)
# Return the name.
return name
def is_even(number):
# Determine whether number is even. If it is, set status
to true. Otherwise, set status to false.
if (number % 2) == 0:
status = True
else:
status = False
# Return the value of the status variable.
return status
number = int(input('Enter a number: '))
if is_even(number):
print('The number is even.')
else:
print('The number is odd.')