Chapter 5 Flashcards
What is a function?
A group of statements that exist within a program for the purpose of performing a specific task
What is a modularized program?
A program that has been written with each task in its own function
List the benefits of using functions in code:
- Code is simpler
- Code can be reused (code reuse)
- Better testing (easier to debug program)
- Faster development in the long run because of code reuse
- Easier to break program down into parts (functions) for teamwork
What is a void function?
executes the statements it contains and then terminates
What is a value-returning function?
executes the statements that it contains, then returns a value back to the statement that called it
-Ex: input functions get value from the user and returns that data as a string
Here is the general format of a function:
def function_name():
statement
statement
etc.
- First line is the function header
- All lines below are indented and are known as a block because they are a group
def message(): print('I am Arthur,') print('King of the Britons.')
What would the following code do here:
message()
message() calls the function above it and executes whatever is in that function
-This is called return
True or False: A function can be called even though it is defined later in the program
True
What is top-down design?
Programming technique to break down algorithms into functions. It works by doing the following:
- The overall task that the program is to perform is broken down into a series of subtasks.
- Each of the subtasks is examined to determine whether it can be further broken down into more subtasks. This step is repeated until no more subtasks can be identified.
- Once all of the subtasks have been identified, they are written in code.
How would you pause the program so the user has time to read everything?
By using an input line that prompts the user to press enter to continue:
input(‘Press Enter to see Step 1.’)
What does “pass” do in functions?
It is a keyword to create empty functions. Later, when the details of the code are known, you can come back to the empty functions and replace the pass keyword with meaningful code. Ex: def step1(): pass
What is a local variable?
A variable that is assigned inside of a function. -A local variable belongs to the function in which it is created, and only statements inside that function can access the variable. - (The term local is meant to indicate that the variable can be used only locally, within the function in which it is created.) -a local variable cannot be accessed by code that appears inside the function at a point before the variable has been created. -Ex This would be an error: def bad_function(): print(f'The value is {val}.') # This will cause an error! val = 99
What is “scope”?
-part of a program in which the variable may be accessed. A variable is visible only to statements in the variable’s scope.
What is an argument?
Pieces of data that are sent into a function are known as arguments. The function can use its arguments in calculations or other operations.
What is a parameter variable?
-special variable that is assigned the value of an argument when a function is called
-Example:
def show_double(number):
result = number * 2
print(result)
-In this case, number is a parameter variable
-What is happening in the following code:
show_double(value)
value is being passed as an argument to the show_double function
What happens to the 12 and 45 in this program?
def main(): 5 print('The sum of 12 and 45 is') 6 show_sum(12, 45) 7 8 # The show_sum function accepts two arguments 9 # and displays their sum. 10 def show_sum(num1, num2): 11 result = num1 + num2 12 print(result) 13 14 # Call the main function. 15 main()
They get passed into the function show_sum int he parameter list order they appear in. So 12 becomes num1 and 45 becomes num2
What does the following program do?
4 def main(): 5 first_name = input('Enter your first name: ') 6 last_name = input('Enter your last name: ') 7 print('Your name reversed is') 8 reverse_name(last=last_name, first=first_name) 9 10 def reverse_name(first, last): 11 print(last, first) 12
This is a keyword argument that uses variables instead.
-Ex line 8 uses last=last_name. In this case, the parameter list order does not matter as seen in line 10
Can you mix keyword arguments and positional arguments in a function call?
Yes, but the positional arguments must appear first
Example:
show_interest(10000.0, rate=0.01, periods=10)
This on the other hand is an error:
show_interest(1000.0, rate=0.01, 10)
What is a global variable?
variable is created by an assignment statement that is written outside all the functions in a program file
- A global variable can be accessed by any statement in the program file, including the statements in any function.
Ex:
1 # Create a global variable.
2 my_value = 10
How do you assign a value to a global variable?
You must declare the global variable in the function: (line 5)` Ex: 1 # Create a global variable. 2 number = 0 3 4 def main(): 5 global number 6 number = int(input('Enter a number: ')) 7 show_number()
Why are global variables generally not used?
-They make programming debugging difficult and make a program hard to understand
What is a global constant?
a global name that references a value that cannot be changed. Because a global constant’s value cannot be changed during the program’s execution, you do not have to worry about many of the potential hazards that are associated with the use of global variables.
What is a library function?
Standard library of functions already written within python
Ex: Input, print, and range
What is a module?
Modules are copied to your computer when you install Python
-They help organize the standard library functions.
-Ex: functions for performing math operations are stored together in a module, functions for working with files are stored together in another module, and so on.
-to call a function that is stored in a module, you have to write an import statement at the top of your program. An import statement tells the interpreter the name of the module that contains the function. For example, one of the Python standard modules is named math.
Ex:
import math
What does import random do? What are some examples of functions within this module?
imports random module and all functions within the random module are now available in your program -Example of functions in this module: number = random.randint (1, 100) number = random.randrange(10) -Note: randint and the randrange functions return an integer number. number = random.uniform(1.0, 10.0)
How are random numbers created in Python?
they are actually pseudorandom numbers that are calculated by a formula. The formula that generates random numbers has to be initialized with a value known as a seed value. The seed value is used in the calculation that returns the next random number in the series.
How do you always generate the same sequence of random numbers?
random.seed(10)
How do you write a value returning function?
Same way you write a void function except it must include a return statement Ex: def function_name(): statement statement etc. return expression
How does the return statement help in value returning functions?
By simplifying the need for variables. Example: def sum(num1, num2): result = num 1 + num 2 return result INSTEAD: def sum(num1, num2): return num 1 + num 2
What is an IPO Chart?
tool that programmers sometimes use for designing and documenting functions. IPO stands for input, processing, and output, and an IPO chart describes the input, processing, and output of a function.
True or False: This function will return a string: def get_name(): # Get the user's name. name = input('Enter your name: ') # Return the name. return name
True
What does the following program do: number = int(input('Enter a number: ')) if (number % 2) == 0: print('The number is even.') else: print('The number is odd.')
Determines whether a number is even or odd
You can return multiple expressions as seen in the following:
first_name, last_name = get_name() def get_name(): # Get the user's first and last names. first = input('Enter your first name: ') last = input('Enter your last name: ') # Return both names. return first, last
What does “none” do?
Used to represent no value -Ex: def divide(num1, num2): if num2 == 0: result = None else: result = num1 / num2 return result
What sort of functions are within the math module?
result = math.sqrt(16) Finds the square root of the value
c = math.hypot(a, b) Finds the hypotenuse
math.pi used for the number pi
math.e used for value e
What is modularization?
Storing related groups of functions into their own module Note: -A module’s file name should end in .py. If the module’s file name does not end in .py, you will not be able to import it into other programs. -A module’s name cannot be the same as a Python keyword. An error would occur, for example, if you named a module for.
def main(): 4 turtle.hideturtle() 5 circle(0, 0, 100, 'red') 6 circle(−150, −75, 50, 'blue') 7 circle(−200, 150, 75, 'green')
Turtle function