Chapter 5 Flashcards

1
Q

What is a function?

A

A group of statements that exist within a program for the purpose of performing a specific task

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a modularized program?

A

A program that has been written with each task in its own function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List the benefits of using functions in code:

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a void function?

A

executes the statements it contains and then terminates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a value-returning function?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Here is the general format of a function:

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
def message():
     print('I am Arthur,')
     print('King of the Britons.')

What would the following code do here:
message()

A

message() calls the function above it and executes whatever is in that function
-This is called return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False: A function can be called even though it is defined later in the program

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is top-down design?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you pause the program so the user has time to read everything?

A

By using an input line that prompts the user to press enter to continue:
input(‘Press Enter to see Step 1.’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does “pass” do in functions?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a local variable?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is “scope”?

A

-part of a program in which the variable may be accessed. A variable is visible only to statements in the variable’s scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an argument?

A

Pieces of data that are sent into a function are known as arguments. The function can use its arguments in calculations or other operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a parameter variable?

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

-What is happening in the following code:

show_double(value)

A

value is being passed as an argument to the show_double function

17
Q

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()
A

They get passed into the function show_sum int he parameter list order they appear in. So 12 becomes num1 and 45 becomes num2

18
Q

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
A

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

19
Q

Can you mix keyword arguments and positional arguments in a function call?

A

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)

20
Q

What is a global variable?

A

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

21
Q

How do you assign a value to a global variable?

A
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()
22
Q

Why are global variables generally not used?

A

-They make programming debugging difficult and make a program hard to understand

23
Q

What is a global constant?

A

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.

24
Q

What is a library function?

A

Standard library of functions already written within python

Ex: Input, print, and range

25
Q

What is a module?

A

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

26
Q

What does import random do? What are some examples of functions within this module?

A
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)
27
Q

How are random numbers created in Python?

A

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.

28
Q

How do you always generate the same sequence of random numbers?

A

random.seed(10)

29
Q

How do you write a value returning function?

A
Same way you write a void function except it must include a return statement
Ex:
def function_name():
     statement
     statement
     etc.
     return expression
30
Q

How does the return statement help in value returning functions?

A
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
31
Q

What is an IPO Chart?

A

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.

32
Q
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
A

True

33
Q
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.')
A

Determines whether a number is even or odd

34
Q

You can return multiple expressions as seen in the following:

A
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
35
Q

What does “none” do?

A
Used to represent no value
-Ex:
def divide(num1, num2):
     if num2 == 0:
         result = None
     else:
         result = num1 / num2
     return result
36
Q

What sort of functions are within the math module?

A

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

37
Q

What is modularization?

A
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.
38
Q
def main():
  4      turtle.hideturtle()
  5      circle(0, 0, 100, 'red')
  6      circle(−150, −75, 50, 'blue')
  7      circle(−200, 150, 75, 'green')
A

Turtle function