ST1 Week 05 Lecture New Updated
ST1 Week 05 Lecture New Updated
def step1():
pass
def step2():
pass
Local Variables (1 of 2)
• 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 (2 of 2)
• 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 (1 of 4)
• Argument: piece of data that is sent into a function
– Function can use argument in calculations
– When calling the function, the argument is placed in
parentheses following the function name
Passing Arguments to Functions (2 of 4)
Figure 5-14 The value variable and the number parameter reference the same value
Passing Multiple Arguments (1 of 2)
• Python allows writing a function that accepts multiple
arguments
– Parameter list replaces single parameter
Parameter list items separated by comma
3. Convert the number of cups to fluid ounces and display the result.
Hierarchy chart
Global Variables and Global Constants
(1 of 2)
• Coding/Testing
Standard Library Functions and the
import Statement (2 of 3)
• Modules: files that stores functions of the standard
library
– Help organize library functions not built into the
interpreter
– Copied to computer when you install Python
• To call a function stored in a module, need to write an
import statement
– Written at the top of the program
– Format: import module_name
Standard Library Functions and the
import Statement (3 of 3)
• Coding/Testing
Writing Your Own Value-Returning
Functions (1 of 2)
• To write a value-returning function, you write a simple
function and add one or more return statements
– Format: return expression
The value for expression will be returned to the part of
the program that called the function
– The expression in the return statement can be a
complex expression, such as a sum of two variables or
the result of another value-returning function
Writing Your Own Value-Returning
Functions (2 of 2)
Figure 5-25 IPO charts for the getRegularPrice and discount functions
Returning Strings
• You can write functions that return strings
• For example:
def get_name():
# Get the user’s name.
name = input(‘Enter your name:’)
# Return the name.
return name
Returning Boolean Values
• Boolean function: returns either True or False
– Use to test a condition such as for decision and
repetition structures
Common calculations, such as whether a number is
even, can be easily repeated by calling a function
– Use to simplify complex input validation code
Returning Multiple Values
• In Python, a function can return multiple values
– Specified after the return statement separated by
commas
Format: return expression1,
expression2, etc.
– When you call such a function in an assignment
statement, you need a separate variable on the left
side of the = operator to receive each returned value
Returning None From a Function
• The special value None means “no value”
• Sometimes it is useful to return None from a function
to indicate that an error has occurred
• Algorithm/Pseudo code:
– 1. Get the salesperson's monthly sales.
– 2. Get the amount of advanced pay.
– 3. Use the amount of monthly sales to determine the commission rate.
– 4. Calculate the salesperson's pay using the formula above. If the amount is negative, indicate that the
salesperson must reimburse the company.
• Coding/Testing
The math Module (1 of 3)
• math module: part of standard library that contains
functions that are useful for performing mathematical
calculations
– Typically accept one or more values as arguments,
perform mathematical operation, and return the result
– Use of module requires an import math statement
The math Module (2 of 3)
Table 5-2 Many of the functions in the math module
def my_function():
statement
statement
if __name__ == '__main__':
main()
Turtle Graphics: Modularizing Code
with Functions (1 of 6)
• Commonly needed turtle graphics operations can be
stored in functions and then called whenever needed.
• For example, the following function draws a square.
The parameters specify the location, width, and color.
def square(x, y, width, color):
turtle.penup() # Raise the pen
turtle.goto(x, y) # Move to (X,Y)
turtle.fillcolor(color) # Set the fill color
turtle.pendown() # Lower the pen
turtle.begin_fill() # Start filling
for count in range(4): # Draw a square
turtle.forward(width)
turtle.left(90)
turtle.end_fill() # End filling
Turtle Graphics: Modularizing Code
with Functions (2 of 6)
• The following code calls the previously shown
square function to draw three squares:
TOP_X = 0
TOP_Y = 100
BASE_LEFT_X = -100
BASE_LEFT_Y = -100
BASE_RIGHT_X = 100
BASE_RIGHT_Y = -100
line(TOP_X, TOP_Y, BASE_LEFT_X, BASE_LEFT_Y, 'red')
line(TOP_X, TOP_Y, BASE_RIGHT_X, BASE_RIGHT_Y, 'blue')
line(BASE_LEFT_X, BASE_LEFT_Y, BASE_RIGHT_X, BASE_RIGHT_Y, 'green')
Summary (1 of 2)
• This topic covered:
– The advantages of using functions
– The syntax for defining and calling a function
– Methods for designing a program to use functions
– Use of local variables and their scope
– Syntax and limitations of passing arguments to
functions
– Global variables, global constants, and their
advantages and disadvantages
Summary (2 of 2)
– Value-returning functions, including:
Writing value-returning functions
Using value-returning functions
Functions returning multiple values
– Using library functions and the import statement
– Modules, including:
The random and math modules
Grouping your own functions in modules
– Modularizing Turtle Graphics Code