# CS 1358 Introduction to Programming in Python SC8
###### tags: `pythonSC`
# Definitions and Short Answers - functions
1. Given a function definiton
```python
def Double(n):
return n + n
```
and given a call to the function
```python
x = Double(20)
```
What is a formal parameter? What is the actual parameter?
> formal parameter : n
> actual parameter : 20
2. If you call the above function as
```python
y = Double( Double(40 + Double(3)) - Double(7))
```
what is the value of y?
> 170
3. What is the difference between
```python
d = Double
```
and
```python
z = Double(20)
```
> Double is function name a callable name.
> Double(20) is a funtcion call
4. If you do
```python
print, input = input, print
z = input('enter a name: ')
```
What happens, and what is the value of z?
>It prints out 'enter a name: ', z = None
5. Given the function
```python
def DivMod(a, b):
return a // b, a % b
```
What does this function return?
> return a tuple(a // b, a % b)
6. Are the following pairs equivalent (in terms of parameters received by the open() function for opening files)?
```python
fh = open('myfile.txt', 'r') and
fh = open(file='myfile.txt', mode='r')
#equivalent
```
```python
fh = open('myfile.txt', 'r') and
fh = open(mode = 'r', name = 'myfile.txt')
#not equivalent, open has no attribute name
```
```python
fh = open('myfile.txt', 'r') and
fh = open('r', 'myfile.txt')
#not equivalent, ordering must match
```
```python
fh = open(file='myfile.txt', mode='r') and
fh = open(mode='r', file='myfile.txt')
#equivalent
```
7. Consider the function declaration
```python=
def withTax(price, rate=0.05):
return price * (1 + rate)
```
Can you call this function in the following ways? If so, what is the result? If not, why not?
```python
withTax(20)
withTax(rate=0.08, price=30)
withTax(price=20, 0.08)
withTax(price=20)
withTax(rate=0.08)
withTax()
withTax(20, 0.08)
```
```python
21.0
32.4
21.6
21.0
#losting parameter
#losting parameter
21.6
```
8. Suppose you are given a function
```python=
def withMoreTax(price, rate = 0.05):
ans = price * (1 + rate)
rate += 0.01
return ans
```
What does the following code sequence print?
```python
print(withMoreTax(20))
print(withMoreTax(20))
```
```python
21
21
```
In other words, does the parameter rate's default value get changed on line 3 such that the next call gets the modified default value?
> No
9. Are you allowed to define default values with non-constant expressions such as the following? If so, what does it do?
```python
import sys
#Yes
def withNewTax(price, rate=input('enter a rate: ')):
return price * (1 + float(rate))
#Yes
def withNewerTax(price, rates=[0.01, 0.02, 0.03]):
ans = price * (1 + rates[-1])
rates.pop()
return ans
#No
def withNewestTax(price, rate=price):
return price * (1 + rate)
```
10. Suppose you have a function declared as
```python
def totalTax(rate, *priceTags):
...
```
What is the value of rate and priceTags inside the function totalTax when you call it as
```python
totalTax(0.05, 10, 20, 23, 18)
totalTax(0.05, (10, 20, 23, 18))
totalTax(0.05, *(10, 20, 23, 18))
totalTax(*(0.05, 10, 20, 23, 18))
totalTax(0.05)
totalTax(0.05, 10)
```
```python
rate : 0.05
priceTags : (10, 20, 23, 18)
rate : 0.05
priceTags : ((10, 20, 23, 18),)
rate : 0.05
priceTags : (10, 20, 23, 18)
rate : 0.05
priceTags : (10, 20, 23, 18)
rate : 0.05
priceTags : ()
rate : 0.05
priceTags : (10,)
```
11. Given the following function definition:
```python=
def totalTax(rate, **priceDict):
return sum(priceDict.values())* (1 + rate)
```
Are the following valid ways of calling the function? If so, what are the values of parameters rate and priceDict while inside the totalTax function, and what is the corresponding return value? If not valid, why not?
```python=
totalTax(0.05, apple=12, orange=8)
totalTax(0.05, 12, 8)
totalTax(0.05, 'apple'=12, 'orange'=8)
totalTax(0.05, 'apple':12, 'orange':8)
totalTax(rate=0.05, priceDict={'apple':12, 'orange':8})
totalTax(rate=0.05, **{'apple':12, 'orange':8})
totalTax(rate=0.05, **priceDict={'apple':12, 'orange':8})
totalTax(rate=0.05, priceDict=(('apple', 12),('orange', 8)))
totalTax(rate=0.05, **priceDict=(('apple', 12),('orange', 8)))
totalTax(apples=12, oranges=8, rates=0.05)
```
```python=
#valid
#invalid
#invalid
#invalid
#invalid
#valid
#invalid
#invalid
#invalid
#invalid
```
12. Given the following function definition
```python=
def totalWithTax(rate, *items, **priceDict):
d={'apple':12,'orange':8, 'mango':3}
d.update(priceDict)
total = 0
for i in items:
total += d[i]
return total * (1 + rate)
```
Are the following valid ways of calling the function? If so, what are the values of parameters rate, items, and priceDict while inside the totalWithTax function, and what is the corresponding return value? If not valid, why not?
```python=
totalWithTax(0.05, 'apple', 'orange', 'mango')
totalWithTax(0.05, 'apple', 'orange', 'mango','apple', 'apple')
totalWithTax(0.05, 'apple', 'orange', 'mango',apple=3, orange=4, mango=5)
totalWithTax(0.05, 'apple', 'orange', 'mango',**{'apple':3, 'orange':4, 'mango':5})
totalWithTax(0.05, *('apple', 'orange', 'mango'))
totalWithTax(0.05)
```
```python=
rate : 0.05, items : ('apple', 'orange', 'mango'), priceDict : {}, return value : 24.15
rate : 0.05, items : ('apple', 'orange', 'mango', 'apple', 'apple'), priceDict : {}, return value : 49.35
rate : 0.05, items : ('apple', 'orange', 'mango'), priceDict : {'apple': 3, 'orange': 4, 'mango': 5}, return value : 12.6
rate : 0.05, items : ('apple', 'orange', 'mango'), priceDict : {'apple': 3, 'orange': 4, 'mango': 5}, return value : 12.6
rate : 0.05, items : ('apple', 'orange', 'mango'), priceDict : {}, return value : 24.15
rate : 0.05, items : (), priceDict : {}, return value : 0.0
```
13. Suppose you have two tuples
```python
A = (1, 3, 7)
B = (2, 4, 8)
```
and you want to use the built-in function max() to find the largest element in the two tuples. Which of the following will correctly find the answer (which is 8)? Choose all that apply and explain why or why not.
```python
max(A, B) #(2, 4, 8)
max(*A, *B) #8
max(A + B) #8
max(*(A+B)) #8
max(*A + *B) #8
max((A, B)) #(2, 4, 8)
max(*(A, B)) #(2, 4, 8)
```
14. Given the following code
```python
a = 3
def F():
print(a)
F()
```
What is printed when you run this code?
> 3
15. Given the following code
```python=
a = 3
def F():
a = 5
print(a)
F()
print(a)
```
What is printed when you run this code?
```python
5
3
```
16. Given the following code
```python=
a = 3
def F():
print(a)
a = 5
F()
print(a)
```
But it gives an UnboundLocalError when you try to run it.
Why do you get this error?
> there is no variable a in function namespace
How do you fix it if you want the function F to use the identifier a as defined on line 1?
```python=
a = 3
def F():
global a
print(a)
a = 5
F()
print(a)
```
17. Given the source code
```python=
D = { 'rate': 0.0 }
def totalWithTax(*names, **kv):
global D
total = 0.0
for name in names:
total += D[name]
for kw, val in kv.items():
D[kw] = val # overwrite dict entry
if kw != 'rate':
total += val
return total * (1 + D['rate'])
```
Are the following valid ways of calling the function totalWithTax? If so, what are the values of parameters names, and kv while inside the totalWithTax function, the values of the global variable D, and what is the corresponding return value? If not valid, why not? Assume you reload the code each time before executing the code below.
```python=
totalWithTax()
totalWithTax('apple')
totalWithTax(apple=20)+totalWithTax('apple')
totalWithTax('orange', orange=15)
totalWithTax(guava)
totalWithTax(rate=0.05, apple=10) + totalWithTax('apple', 'apple', 'apple')
totalWithTax(apple=10, orange=15)+totalWithTax('apple',guava=12)
totalWithTax(apple=10, orange=15) + totalWithTax(rate=0.05, guava=12) + totalWithTax(rate=0.02, 'apple', 'orange', 'guava')
```
```python=
#Valid
#Invalid
#Valid
#Invalid
#Invalid
#Valid
#Valid
#Invalid
```
18. How do you write test code at the end of a module to test functions defined in the module when it is run as a top-level module (hint: '__main__'), but don't run the test case when it is imported by another module? Also, what construct should you use to check if a tested function returns the results as the correct answer?
```python=
if __name__ == '__main__':
#testing part
```