Week 9: Modular Flashcards
Define: Functions
block of code that does the same thing, repeating
T or F:
A definition without a call never runs
True
What are they called, that go inside the parenthesis with defining functions?
Example:
my_nums(1,2):
Arguments
Define: Positional argument
An argument that is passed to a function based on the order in which it appears. The position of each argument in the function call corresponds to the position of the parameters in the function definition. Example: side_hoe(2,3)
Define: Parameter
A parameter is a variable in the declaration of a function.
It acts as a placeholder for the value that will be passed to the function when it is called.
Define: Keyword argument
Not matching arguments to parameters. The functions parameters match all the keys in the function call, not the order4 of arguments.
T or F:
Only keyword parameters can have a default value
True
Name each section unlisted:
def function_name (parameters):
#statement body
return expression
def = keyword
#statement body = code to execute/function body
expression = expression returned by the function
In the following code, what is the value of total if the function call doesn’t pass any arguments?
def add_numbers(first_number = 2, second_number = 4):
total = first_number + second_number
6
In the following code, what is the value of total?
def add_numbers(first_number = 2, second_number = 4):
total = first_number + second_number
add_numbers(second_number = 10)
12
T or F:
Parameters with defaults must come before parameters without defaults
False
Parameters with defaults must come after parameters without defaults. Example:
this is incorrect - def x(y = 1, z)
Call this function. Don’t override the defaults.
def x(y = “Bulgaria”, z = 1.89):
x()
Code a call to this function that makes full_name equal “Hugo Jones”. Don’t pass any information to the function that you don’t have to pass.
def assemble_name(last_name = “Jones”, first_name = “John”):
full_name = first_name + “ “ + last_name
(re-assign variable/argument) assemble_name(first_name = “Hugo”)
Which is positional?
Give_greeting(“Hello”, first_name=”Al”)
The “Hello” is positional because theres no keyword, no defining variable name
T or F:
Positional arguments must come before keyword arguments
True
How to find a variable from a dictionary
find_something(customers_list_name, 2_is_customer_id, “last name”)
Function definition would be:
Def find_something(dict, inner_dict, target):
print(dict[inner_dict][target])
Call the function calc, providing x, a positional argument, and a keyword argument. The key is y, the value is 0.
calc(x, y = 0)
Here is the first line of a function and the code that calls it. Rewrite the calling code so it’s legal.
def calc(first_number, second_number):
calc(second_number = 10, 8
calc(8, second_number = 10)
The positional argument has to line up
with the positional parameter
Which is a parameter? Argument?
(x,y)
(2,4)
Parameter
Argument
How to show/hide optional keyword arguments? What does the function do with it?
**other_info
Put it in a dictionary
T or F:
Regular arguments must come after optional arguments
False
Optional arguments must come after regular arguments. Optional parameters must come after regular parameters
How to handle optional positional (or keyword) arguments?
*other_info
To pass information back from a function to the code that called it, you use the keyword ______
return
The calling code needs to provide a _______ that will receive the value that is returned by the function
variable