6 Defining Functions Flashcards
How to import all the functions in a library?
from function import *
What are the two drawbacks of not using functions?
having to write the code twice, the same code has to be maintained at two different places
What is “a function definition”?
A part of the program to creates a function
What does it mean for a function to be “called” or “invoked”?
The function is used in a program
What is “the scope of a variable”?
Refers to the places in a program where a given variable may be referenced
Discuss the scope of variables used inside a function.
They are local to that function.
What is only way for a function to see a variable from another function?
Passing the variable as a parameter
How does “a function definition” look like?
def ():
What are “formal parameters”?
They are variables only accessible in the function body (subset of local variables).
How to call a function?
()
What happens after a function call (five steps)?
(1. Calling program suspends execution.)(2. Function definition is looked up.)(3. Formal parameters get assigned by values of actual parameters.)(4. The function body is executed.)(5. Control returns to the point of function call.)
How are formal and actual parameters matched?
By position
How to use “keyword parameters”?
def ( = ): , ( = ), match my name
Where do fundamental ideas and vocab for functions come from?
From mathematical functions
What two things happen after control reaches “return” statement?
(1. exits and returns to the point of function call.)(2. The value provided in the return statement is sent back to caller.)
In what form is the value in return statement returned to caller?
As expression
Why are value-returning functions extremely useful?
They are more elegant and flexible.
How to print in a file?
(outf = open(‘something.txt’, ‘w’))(print(something, file=outf))(outf.close()
How to return more than one value?
return ,
How to achieve simultaneous assignment?
, = return ,
What do “functions without a return” return?
None
What is the main way to send information from a function back to its caller?
Return values
Does the called function have access to the variable that holds the actual parameter?
No. The formal parameter only receive the values of actual parameters.
Does python pass all parameter by value?
Yes
Why can lists be changed inside a function?
Because a list is mutable, that is, it is only the vehicle to values, not the actual values themselves
Why are functions used even when they sometimes make programs longer?
Because they make programs more modular.