Homework 4 - Functions
Due Date: October 17, 2023
When writing a function, do not ask the user for input. In all homework problems in the rest of this course, no input will be needed from the user unless explicitly specified in the problem. If you want to test a function that you wrote then simply call it with arguments and print the result to the screen.
Problems
- Answer the following questions:
- In Python, the keyword
def
is used for what purpose? - In a function, what is the difference between an argument and a parameter?
- What are keyword arguments? When would you use a keyword argument?
- In Python, what is the keyword
global
used for? You will hardly ever need to useglobal
in this course.
- In Python, the keyword
- Consider the following fragment of code:
def my_polynomial(x): result = 2 * x * x - 3 * x - 7 return result my_polynomial(8) print(f"The result is {result}")
What type of error do you get? Explain why this is the case. Then fix the error by calling the function and saving the value returned by the function in the variable $y$ and then printing the value of $y$ to the screen. Do not use the keyword
global
inside the function to solve this problem. Write a function that has two parameters which are both positive integers, say \(m\) and \(n\), and prints an array containing \(m\) rows and \(n\) columns where in an even row the array contains \(n\) asterisks and the odd rows contain \(n\) plus signs. Call your function
my_grid
. As an example, the following is produced with a call tomy_grid(5, 12)
:
while the following is produced with a call to************
++++++++++++
************
++++++++++++
************my_grid(10, 4)
:****
++++
****
++++
****
++++
****
++++
****
++++- Before proceeding with this problem, you will need to have a working solution to Problem 5 from Homework 3.
In your airline ticket reservation system, one of the main tasks of your program is to display to the user the connecting flight options. If the customer selects a morning flight then you display the three connecting flights out of JFK and if they select an afternoon flight then you display the two connecting flights out of BOS.- Define a function, giving it an appropriate name, that takes no arguments, has no return value, and simply prints to the screen the connecting flight options for flights departing from JFK.
- Define another function, giving it an appropriate name, that does the same for flights departing from BOS.
- Use both functions into your airline reservation system. The functions should be defined outside your main flight reservation program.