Chapter 7: Functions and Modules Flashcards
What is the syntax for defining your own function?
def functionName(list of parameters): code detailing what the function should do return [expression]
Explain the syntax
def tells the program that indented code from next line onwards is part of the program. Return is the keyword used to return an answer from the function.
Can there be more than 1 return statement in a function?
Yes
What happens once the function executes a return statement?
It exits
What to do if the function need not return any value?
We can omit the return statement. Alternatively, we can write return or return none.
Write the script for defining a function.
What happens if the local variable shares the same name as the global variable?
Any code inside the function is accessing the local variable. Any code outside the function is accessing the global variable.
Write a script with the result showing the last flashcard
Result:
INSIDE THE FUNCTION Local variable(shares the same name as a global variable)
OUTSIDE THE FUNCTION Global variable(shares same name as a local variable)
What does Python allowing a default value mean?
This means that Python allows to assign default values to the parameters when defining a function
Give an example for defining the default parameters
def someFunction(a,b,c=1,d=2,e=3): print(a,b,c,d,e)
What are salient features to remember when assigning default parameters?
1) All parameters with default values must be placed at the end of the parameters list.
How does Python allow us to pass a variable number of arguments to a function?
By using the *(asterisk) before the parameter
Give an example of variable length argument
When we add an asterisk in front of a parameter name, we can pass in a variable number of arguments to the function. This is known as a ________variable length argument list.
non-keyworded
If we want to pass in a keyworded variable length argument list to the function, we can use _______
double asterisks