Chapter 5: Function
5.1 What is a function?
- A function is a group of statements that exist within a program for the purpose of
performing a specific task.
5.2 What is meant by the phrase “divide and conquer”?
- A large task is divided into several smaller tasks that are easily performed.
5.3 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.
5.4 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.
5.5 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.
Calling a VOID function
5.6 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.
5.7 What does the phrase “calling a function” mean?
- To call a function means to execute the function.
5.8 When a function is executing, what happens when the end of the function’s block is
reached?
- 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.
5.9 Why must you indent the statements in a block?
- Because the Python interpreter uses the indentation to determine where a block begins
and ends
5.10 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.
5.11 What is a variable’s scope?
- The part of a program in which a variable may be accessed
5.12 Is it permissible for a local variable in one function to have the same name as a local
variable in a different function?
- Yes
Passing arguments to Functions
5.13 What are the pieces of data that are passed into a function called?
- arguments
5.14 What are the variables that receive pieces of data in a function called?
- parameters
5.15 What is a parameter variable’s scope?
- A parameter variable’s scope is the entire function in which the parameter is declared.
5.16 When a parameter is changed, does this affect the argument that was passed into the
parameter?
- No
5.17 The following statements call a function named show_data. Which of the statements
passes arguments by position, and which passes keyword arguments?
1. show_data(name='Kathryn', age=25)keyword argument
2. show_data('Kathryn', 25)position
5.18 What is the scope of a global variable?
- the entire program
5.19 Give one good reason that you should not use global variables in a program.