Chapter 5 Flashcards
(38 cards)
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