Find study materials for any course. Check these out:
Browse by school
Make your own
To login with Google, please enable popups
To login with Google, please enable popups
Don’t have an account?
To signup with Google, please enable popups
To signup with Google, please enable popups
Sign up withor
What is a function?
A function is a group of statements that exist within a program for the purpose of performing a specific task.
What is meant by the phrase “divide and conquer?”
A large task is divided into several smaller tasks that are easily performed.
How do functions help you reuse code in a program?
If a specific operation is performed in several places in a program, a function can be written once to perform that operation and then be executed any time it is needed.
How can functions make the development of multiple programs faster?
Functions can be written for the common tasks that are needed by the different programs. Those functions can then be incorporated into each program that needs them.
How can functions make it easier for programs to be developed by teams of programmers?
When a program is developed as a set of functions in which each performs an individual task, then different programmers can be assigned the job of writing different functions.
A function definition has what two parts?
A function definition has two parts: a header and a block. The header indicates the starting point of the function, and the block is a list of statements that belong to the function.
What does the phrase “calling a function” mean?
To call a function means to execute the function.
When a function is executing, what happens when the end of the function’s block is reached?
When the end of the function is reached, the computer returns back to the part of the program that called the function, and the program resumes execution at that point.
Why must you indent the statements in a block?
Because the Python interpreter uses the indentation to determine where a block begins and ends.
What is a local variable? How is access to a local variable restricted?
A local variable is a variable that is declared inside a function. It belongs to the function in which it is declared, and only statements in the same function can access it.
What is a variable’s scope?
The part of a program in which a variable may be accessed
Is it permissible for a local variable in one function to have the same name as a local variable in a different function?
Yes, it is permissible.
What are the pieces of data that are passed into a function call?
What are the variables that receive pieces of data in a function header called?
What is a parameter variable’s scope?
A parameter variable’s scope is the entire function in which the parameter is declared.
When a parameter is changed, does this affect the argument that was passed into the parameter?
No, it does not.
The following statements call a function named show_data. Which of the statements passes arguments by position, and which passes keyword arguments?
a) show_data(name='Kathryn', age=25)
b) show_data('Kathryn', 25)
a. passes by keyword argument
b. passes by position
What is the scope of a global variable?
The entire program
Explain one good reason why global variables should not be used in a program.
Global variables make debugging difficult. Any statement in a program can change the value of a global variable. If you find that the wrong value is being stored in a global variable, you have to track down every statement that accesses it to determine where the bad value is coming from. In a program with thousands of lines of code, this can be difficult.
What is a global constant? Is it permissible to use global constants in a program?
A global constant is a name that is available to every function in the program. It is permissible to use global constants. Because their value cannot be changed during the program’s execution, you do not have to worry about its value being altered.
A(n) _________ is a group of statements that exist within a program for the purpose of performing a specific task.
Most programs perform tasks that are large enough to be broken down into several _________. For this reason, programmers usually break down their programs into small manageable pieces known as functions.
Most programs perform tasks that are large enough to be broken down into several subtasks. For this reason, programmers usually break down their programs into small manageable pieces known as __________.
A(n) ________ is a group of statements that exist within a program for the purpose of performing a specific task. Instead of writing a large program as one long sequence of statements, it can be written as several small ________s, each one performing a specific part of the task. These small ________s can then be executed in the desired order to perform the overall task.
A function is a group of statements that exist within a program for the purpose of performing a specific _____. Instead of writing a large program as one long sequence of statements, it can be written as several small functions, each one performing a specific part of the _____. These small functions can then be executed in the desired order to perform the overall _____.
The approach to programming that involves writing several small functions which each perform a specific part of an overall task is sometimes called _______ _____ ________ because a large task is divided into several smaller tasks that are easily performed.
When using functions in a program, you generally isolate each ______ within the program in its own function.
A program that has been written with each task in its own function is called a(n) _____________ _________..
Simpler Code: Several small functions are much easier to read than one long sequence of statements; Code Reuse: functions reduce the duplication of code within a program; Better Testing: programmers can test each function in a program individually to determine whether it correctly performs its operation; Faster Development: functions can be used in different programs that require same operation; Easier Facilitation of Teamwork: different programmers can write different functions
The code for a function is known as a(n) _________ ___________. To execute the function, you write a statement that calls it.
When ________ functions, Python requires that you follow the same rules that you follow when ________ variables.
Because functions perform actions, most programmers prefer to use ______ in function names. Each function name describes what the function does.
To create a function you write its ____________.
The function header begins with the key word def, followed by the name of the function, followed by a set of parentheses, followed by a colon.
All of the statements in a block are ____(a)____. This _____(b)_____ is required because the Python interpreter uses it to tell where the block begins and ends.
Because the Python interpreter uses it to tell where the block begins and ends.
A function definition specifies what a function does, but it does not cause the function to execute.
To execute a function, you must _____ it.
When a function is _______, the interpreter jumps to that function and executes the statements in its block.
When a function is called, the interpreter jumps to that function and executes the statements in its ______.
When the end of a function's block is reached, the interpreter jumps back to the part of the program that called the function, and the program resumes execution at that point. When this happens, we say that the function ________.
It is common for a program to have a(n) _____ function that is called when the program starts. The _____ function then calls other functions in the program as they are needed. It is often said that the _____ function contains a program’s mainline logic, which is the overall logic of the program.
It is common for a program to have a main function that is called when the program starts. The main function then calls other functions in the program as they are needed. It is often said that the main function contains a program’s _________ ______, which is the overall logic of the program.
When a program calls a function, programmers commonly say that the _______ of the program transfers to that function. This simply means that the function takes _______ of the program’s execution.
In Python, each line in a block must be ___________.
When you indent the lines in a block, make sure each line begins with the same number of _______.
Programmerscommonlyuseatechniqueknownas ____-_____ ________ to break down an algorithm into functions.
How is a function call shown in a flowchart?
Do programmers typically draw a separate flowchart for each function in a program or are all functions expressed in a single flowchart?
When drawing a flowchart for a function, the starting terminal symbol usually shows the name of the function and the ending terminal symbol usually reads Return.
What technique do programmers commonly use to break an algorithm into functions?
• 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.
This process is called top-down design because the programmer begins by looking at the topmost level of tasks that must be performed, and then breaks down those tasks into lower levels of subtasks.
Flowcharts are good tools for graphically depicting the flow of logic inside a function, but they do not give a visual representation of the relationships between functions. Programmers commonly use ___________ _______ for this purpose.
A hierarchy chart, which is also known as a structure chart, shows boxes that represent each function in a program. The boxes are connected in a way that illustrates the functions called by each function.
The hierarchy chart does not show the steps that are taken inside a function. Because they do not reveal any details about how functions work, hierarchy charts do not replace _(a)__-_______ or _____(b)_____.
Sometimes you want a program to pause so the user can read information that has been displayed on the screen. When the user is ready for the program to continue execution, he or she presses the Enter key and the program resumes. In Python you can use the _______ function to cause a program to pause until the user presses the Enter key.
Sometimes you want a program to pause so the user can read information that has been displayed on the screen. When the user is ready for the program to continue execution, he or she presses the ______ key and the program resumes. In Python you can use the input function to cause a program to pause until the user presses the ______ key.
A local variable is created inside a function and cannot be accessed by statements that are outside the function.
Anytime you assign a value to a variable inside a function, you create a(n) ______ _________. A(n) ______ _________ belongs to the function in which it is created, and only statements inside that function can access it.
An argument is any piece of data that is passed into a function when the function is called.
A parameter is a variable that receives an argument that is passed into a function.
Pieces of data that are sent into a function are known as ___________. The function can use its ___________ in calculations or other operations.
If you want a function to receive arguments when it is called, you must equip the function with one or more ______(a)__ __________s. A(n) ______(a)__ __________, often simply called a(n) ____(b)____, is a special variable that is assigned the value of an argument when a function is called.
The first argument is passed to the first parameter variable, and the second argument is passed to the second parameter variable, and so on.
The form of argument passing that is used in Python, where a function cannot change the value of an argument that was passed to it, is commonly called _____ ___ ______. This is a way that one function can communicate with another function. The communication channel works in only one direction, however. The calling function can communicate with the called function, but the called function cannot use the argument to communicate with the calling function. This is true of functions that do not return values.
In addition to the conventional form of argument passing, the Python language allows you to write an argument in the following format, to specify which parameter variable the argument should be passed to:
An argument that is written in accordance with this syntax is known as a(n) _________ __________.
It is possible to mix positional arguments and keyword arguments in a function call, but the ____(a)____ arguments must appear first, followed by the ____(b)____ arguments. Otherwise an error will occur.
A(n) ________ variable is accessible to all the functions in a program file.
When a variable is created by an assignment statement that is written outside all the functions in a program file, the variable is _______. A(n) _______ variable can be accessed by any statement in the program file, including the statements in any function.
An additional step is required when creating a global variable if you want a statement in a function to assign a value to the global variable. What is this additional step?
Most programmers agree that it is acceptable to use global variables.
In most cases, you should create variables ________ (rather than globally) and pass them as arguments to the functions that need to access them.
In most cases, you should create variables locally (rather than ________) and pass them as arguments to the functions that need to access them.
Although you should try to avoid the use of global variables, it is permissible to use global _________s in a program. A global _________ is a global name that references a value that cannot be changed. Because a global _________’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.
Although the Python language does not allow you to create true global constants, you can simulate them with global variables. How is this done?
A group of statements that exist within a program for the purpose of performing a specific task is a(n) __________.
A design technique that helps to reduce the duplication of code within a program and is a benefit of using functions is __________.
The first line of a function definition is known as the ________.
You _____ the function to execute it.
A design technique that programmers use to break down an algorithm into functions is known as __________.
A(n) __________ is a diagram that gives a visual representation of the relationships between functions in a program.
A(n) __________ is a variable that is created inside a function.
A(n) _______ is the part of a program in which a variable may be accessed.
A(n) __________ is a piece of data that is sent into a function.
A(n) __________ is a special variable that receives a piece of data when a function is called.
A variable that is visible to every function in a program file is a(n) __________.
When possible, you should avoid using _________ variables in a program.
The phrase “divide and conquer” means that all of the programmers on a team should be divided and work in isolation.
Functions make it easier for programmers to work in teams.
Function names should be as short as possible.
Calling a function and defining a function mean the same thing.
A flowchart shows the hierarchical relationships between functions in a program.
A hierarchy chart does not show the steps that are taken inside a function.
True or False?
A statement in one function can access a local variable in another function.
In Python you cannot write functions that accept multiple arguments.
True or False?
In Python, you can specify which parameter an argument should be passed into in a function call.
Name and describe the two parts of a function definition.
Sign up for free and study better.
Get started today!