Programming Chapter 6 Test
What does the following code do? def func( ): print ("A function at your service.")
displays: A function at your service. (or, nothing, if it is not called)
True or False: A function must receive at least one value and return at least one value.
false
What will the following code display? def test(): a = 10 print (a, end = "") a = 5 test() print (a)
5
Parameter:
Collecting the value from a previous function. In the parenthesis .
Docstring:
A string with triple quotes.
Abstraction:
Abstraction is a concept that lets you think about the big picture without worrying about the details.
Function Definition
Anything from the "def *insert function name here* to the final line in the def or the return.
True or False: An argument and a parameter are two names for the same thing.
False
Function Header
First line; where the function name is being defined. ex) def display(a, b)
What does the following code display? def rate(score): if score < 50: message = "You should keep studying" elif score < 80: message = "You can stop studying now, if you want." else: message = "Go watch some TV!" return message print (rate(95))
Go watch some TV!
Encapsulation:
Helps keep independent code truly separate by hiding or encapsulating the details.
Argument:
Line of code where variables are returned from the functions using the return function.
Scopes:
The scope of a variable refers to the places that you can see or access a variable. ex) local scope is a local variable in its respective function.
Return Value:
Value(s) that are returned from the function using the return function. It can be used in the next function.
Global Variable:
Variable that can be used all throughout the program; in any function definition. Can only be changed if global is put before the variable name and then it is redefined.
Local Variable:
Variable that can only be used in the function.
what is a mechanism that lets you think about the big picture without worrying about the details.
abstraction
Name two advantages of using functions.
can use any variable in different functions easier to use when working with a group
What keyword begins a function definition?
def
How many local variables are in the following code? How many global variables are in the following code? def test1(): var1 = 1 def test2(): var2 = 2 var3 = 3
local variables: 2 global variables: 1
How is a docstring similar to and different from a comment?
the program ignores; it is there to help the coder