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