Lab 6: Functions

This lab will help you get practice using functions. It will also help you understand how functions are specified. From now on most of the things that I will have you write will be in a function. It’s important that you structure your function to match the question exactly.

Here’s an example of a function specification. It tells you the name of the function, what it should return the names of the arguments (if any) and their order. Though the argument names can change everything else should be the same.

  • Name: the_function

  • Arguments:

    • arg1 (string) - The first argument

    • arg2 (integer) - The second argument

  • Return Values:

    • Your name (string)

Here’s how you setup the answer:

def the_function(arg1, arg2):
    """This is the docstring for the_function"""
    # Put your code here
    return "Mike"

Before you turn in or test your function make sure that:

  1. You used the correct name

  2. There is a docstring

  3. You have the same number of arguments (you can rename them if you like)

  4. You return the requested value. If the requested value is None you can simply return

Part 1: Writing Functions

1.1. Write foo

Write a function called foo that takes one argument bar. The function should print the contents of bar.

  • Name: foo

  • Arguments:

    • bar (string) - The thing to print

  • Returns: None

[ ]:

Test your function using the cell below:

[ ]:

1.2. Write do_sum

Write a function called do_sum that takes two arguments, a and b. The function should print the sum of its arguments.

  • Name: do_sum

  • Arguments:

    • a (float) - A number

    • b (float) - Another number

  • Returns: None

[ ]:

Test your function in the cell below:

[ ]:

1.3. Print Repeated

Write a function called print_repeat that takes two arguments, words and times. The function prints words times times.

  • Name: print_repeat

  • Arguments:

    • words (string) - The words to repeat

    • times (integer) - Another number

  • Returns: None

[ ]:

Test your function is the cell below:

[ ]:

1.4. A Return Value

Write a function called i_am_four that returns the number 4.

  • Name: i_am_four

  • Arguments: None

  • Returns: 4

[ ]:

Test your function in the cell below:

[ ]:

1.5. Arguments and Return Values

Write a function called do_sum_return that takes two arguments, a and b. The function returns the sum of the two numbers.

  • Name: do_sum_return

  • Arguments:

    • a (float) A number

    • b (float) Another number

  • Returns: The sum of a and b

[ ]:

Test your function in the cell below:

[ ]:

1.6. Returning a Boolean Value

Write a function called is_greater that takes two arguments called a and b. The function returns True if a is greater than b, False otherwise.

  • Name: is_greater

  • Arguments:

    • a (float) A number

    • b (float) A number

  • Returns:

    • True if a is greater than b. False otherwise.

[ ]:

Test your function in the cell below:

[ ]:

1.7. Read a Whole File

Write a function called read_file that has an argument filename that contains the name of a file. The function should read the file and return it’s entire contents. The function must close the file before returning.

  • Name: read_file

  • Arguments:

    • filename (string) The name of a file to read.

  • Returns: (string) The contents of the file.

[ ]:

Test your function below. You can use the file example.txt for testing purposes.

[ ]:

1.8. Write a Whole File

Write a function called write_file that takes two arguments filename and contents. The function opens the file named in filename removing any previous contents and replacing them with contents. Return the number of characters written to the file. The function must close the file before returning.

  • Name: write_file

  • Arguments:

    • filename (string) The name of a file to write.

    • contents (string) The stuff to write into the file.

  • Returns: (integer) The number of characters written.

[ ]:

Test your function in the cell below. Write to a file named output.txt

[ ]:

1.9. Input and Functions

Write a function called input_number that prompts the user for a number and returns the user input as a float. Bonus: use a ``try`` and an ``except`` to detect an error. If there’s an error return ``0``.

  • Name: input_number

  • Arguments: None

  • Returns: (float) The number the user typed

[ ]:

Test your function in the cell below.

[ ]:

1.10: Summing Values

Write a function called file_sum that takes one argument filename, the name of a file. The function should read the first four lines of the file and convert the lines to float, then return the sum of the numbers in the file.

  • Name: file_sum

  • Arguments:

    • filename (string) - The name of a file

  • Returns: (float) The sum of the first four lines in the file.

[ ]:

Test your function in the code below. The file numbers.txt in the current directory can be used for testing.

[ ]:

1.11. Top to Bottom

Write a function called top_to_bottom that takes one argument filename. The function reads the file and re-rewrites it with the first line of the file moved to the bottom. For example if the file looked like this before top_to_bottom:

line 1
line 2
Mary had a little
Lamb.
And stuff

After top_to_bottom the file should look like this:

line 2
Mary had a little
Lamb.
And stuff
line 1
  • Name: top_to_bottom

  • Arguments:

    • filename (string) - The name of a file

  • Returns: None

[ ]:

Test your function in the cell below:

[ ]: