Week 9: Modular Flashcards

1
Q

Define: Functions

A

block of code that does the same thing, repeating

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

T or F:
A definition without a call never runs

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are they called, that go inside the parenthesis with defining functions?
Example:
my_nums(1,2):

A

Arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define: Positional argument

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define: Parameter

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define: Keyword argument

A

Not matching arguments to parameters. The functions parameters match all the keys in the function call, not the order4 of arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

T or F:
Only keyword parameters can have a default value

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Name each section unlisted:
def function_name (parameters):
#statement body
return expression

A

def = keyword
#statement body = code to execute/function body
expression = expression returned by the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)

A

12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

T or F:
Parameters with defaults must come before parameters without defaults

A

False
Parameters with defaults must come after parameters without defaults. Example:
this is incorrect - def x(y = 1, z)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Call this function. Don’t override the defaults.
def x(y = “Bulgaria”, z = 1.89):

A

x()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

(re-assign variable/argument) assemble_name(first_name = “Hugo”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which is positional?
Give_greeting(“Hello”, first_name=”Al”)

A

The “Hello” is positional because theres no keyword, no defining variable name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

T or F:
Positional arguments must come before keyword arguments

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to find a variable from a dictionary

A

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])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Call the function calc, providing x, a positional argument, and a keyword argument. The key is y, the value is 0.

A

calc(x, y = 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

calc(8, second_number = 10)
The positional argument has to line up
with the positional parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Which is a parameter? Argument?
(x,y)
(2,4)

A

Parameter
Argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to show/hide optional keyword arguments? What does the function do with it?

A

**other_info
Put it in a dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

T or F:
Regular arguments must come after optional arguments

A

False
Optional arguments must come after regular arguments. Optional parameters must come after regular parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How to handle optional positional (or keyword) arguments?

A

*other_info

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

To pass information back from a function to the code that called it, you use the keyword ______

A

return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

The calling code needs to provide a _______ that will receive the value that is returned by the function

A

variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
To pass the result of the math operation back to the calling code, code the last line of the function. def calc(): total = 2 + 2
return total
26
Call the function calc, which will return a value. The variable that will receive the returned value is named result
result = calc()
27
Code a function, named trivial, that does nothing but pass the value 2 back to the calling code
def trivial(): return 2
28
Call a function that receives a value returned by the function. The function call has no arguments
result = hello()
29
Code a function that adds 2 plus 2, assigns the result to a variable, and passes the result back to the calling code. Make up the function name and variable
def calc(): total = 2 + 2 return total
30
Code a function that adds 2 + 2, but condense the code into two lines. Don't assign 2 + 2 to a variable. Pass the result itself back to the calling code. Make up the name of the function
def calc(): return 2 + 2
31
Call a function, but this time, instead of assigning the returned value to a variable, just display the returned value.
print(calc())
32
Code a three-line function that creates a list with three integers. Pass the list back to the calling code
def make_list(): numbers = [12, 24, 36] return numbers
33
Condense: sales_tax = calc_tax(sales+total=101.37, tax+rate=.05) print(sales_tax)
Print(calc_tax(sales+total=101.37, tax+rate=.05))
34
Define: global variable
(global scope) variable defined in the main body of the code and not in a function
35
Define: local variable
(local scope) defined in a function
36
Which takes precedence, global or local variable with the same name and the global keyword is not being used?
local
37
Name the outcome: greeting = "Hello" def say_hello(): #Print a greeting greeting = "Bonjour" print(greeting) say_hello() print(greeting)
Bonjour Hello
38
T or F: When there are a local and global variable with the same name and the global keyword is being used, then the global scope variable takes precedence
True
39
What is the value of y def x(num): a = 2 return num * a y = x(10)
20 because inside the function, a is assigned to 2. The function returns the result of num multiplied by a
40
In the following code, what is the final value of a? def a(): b = 2 return b a = 1 a = a(
2 because of the return statement
41
If a function defines the variable first_name as "George" and code outside the function defines the variable first_name as "Hamid", and the function code says, print(first_name), what name will display?
George
42
If a function defines the variable first_name as "George" and code outside the function defines the variable first_name as "Hamid", and the main code says, print(first_name), what name will display?
Hamid
43
What are the 2 types of functions?
Perform a task Return a value
44
What is modular programming?
breaking a program into smaller pieces. Pay more attention to data flow.
45
2 types of modules are...
Procedures (do not return data[non value returning]) and Functions (ability to return data)
46
What questions should be considered when coding functions?
Input, outputs, and does the module need data, does the data need to return? How do we break this problem into separate pieces? For each piece, what data does the module need in order to perform what it needs to do?
47
Define: a module
unit of code with a distinct purpose
48
Define: procedure
module that does not return a value
49
Define: function
module that returns a value
50
Define: main()
a module, calls other modules. Python does not require mains. (seems to be the calling line of code, I think). If the purpose is defined, like generating random numbers or doing calculations, main will need to be defined.
51
Define: calling
line of code that initiates module that has been defined (makes the module active). Can be done after a module has been defined.
52
Define: defining
process by which we create a module of code. When we define, we give it a name, and specify the data needs. Specifies what data the module returns.
53
Define: arguments
located in the call statement and specify what data is going into a module
54
Define: "by value"
import parameters, a copy of the data sent in. memory allocation is not sent in, only the value. If a change happens to the parameters, it is only reflected in the module. Like a throw away-able ziploc bag for pizza
55
Define: "by reference"
export parameter, memory address is also sent in so the contents can be changed. Value is set in. More efficient than using a global variable. Like a returnable glass container for pizza. Python does not allow this type
56
T or F: Python, like other languages, won't allow you to return multiple or no values
False
57
Define: recursion
(return it to the function?) module of code that calls itself, like a nested loop. Solving a coding problem with modules involves breaking down the problem into smaller problems. When these smaller problems are variations of the larger problem (also know as self-similar), then recursion can be used. When something is returned, it will go to the calculation code, the recursion, that has the function name in it.
58
Define: factorial
factorial of a number is the product of all positive integers up to that number. Example: 5!=5×4×3×2×1=120 Each recursive module has two parts: the recursive case (where the module calls itself with a different parameter) and the base case (where the module stops calling itself and returns a value). The base case is the most important part of a recursive function. Without it, the module will never stop calling itself.
59
Define: Fibonacci sequence
Set numbers - a number in which the current number is the sum of the previous 2 Fibonacci numbers. 0 + 1, 1 +1, 1+ 2, 2+3, etc.
60
How to round a decimal in Python?
your_variable = a number goes here your_variable = round(your_variable, 2) print(your_variable)
61
What will the turtle create? def shape(size): for _ in range(6): turtle.forward(size) turtle.left(60)
Hexagon
62
/t does what?
tabs
63
** tells Python to do what?
exponentiation
64
Define: factorizating
with the shrunk code, ad functions to remove duplications