Programming

MATH 230 : Fall 2023

Department of Mathematics - SUNY Geneseo
⇐ Back

Homework 4 - Functions

Due Date: October 17, 2023

Upload

Due by 11:59 pm on the due date.

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

  1. Answer the following questions:
    1. In Python, the keyword def is used for what purpose?
    2. In a function, what is the difference between an argument and a parameter?
    3. What are keyword arguments? When would you use a keyword argument?
    4. In Python, what is the keyword global used for? You will hardly ever need to use global in this course.
  2. 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.

  3. 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 to my_grid(5, 12):

    ************
    ++++++++++++
    ************
    ++++++++++++
    ************
    while the following is produced with a call to my_grid(10, 4):
    ****
    ++++
    ****
    ++++
    ****
    ++++
    ****
    ++++
    ****
    ++++
  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.
    1. 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.
    2. Define another function, giving it an appropriate name, that does the same for flights departing from BOS.
    3. Use both functions into your airline reservation system. The functions should be defined outside your main flight reservation program.