CPSC 110-08: Computing with Mobile Phones

Homework 8: Writing Functions, Part II
Due: Friday 10/21 (before class)

CS Principles

This reading and homework activity focuses on abstraction again, this time focusing on functions and parameters. The student will be introduced to functions as re-usable programming abstractions and will see how parameterization can be used to generalize a specific solution. It begins to address the following learning objectives.

Writing and Testing Functions

Review the basics of writing functions here: Functions Tutorial:

For each of the following exercises, first write the function in Pseudocode and then implement it and test it in App Inventor. Post your solutions to this homework on a Portfolio page.

  1. Write a function named sumseries that computes the sum of the numbers from 1 to N. This function should take 1 parameter, N, which represents the upper bound of the series. For example, sum(5) would return the value 15, which is the sum of 1 to 5 inclusive. .

  2. Write a function named factorial that computes the factorial of N. Recall that the factorial function is defined as follows:
    factorial(N) = 1, if N = 0
    factorial(N) =  N * N-1 * N-2 * ... * 2 * 1, if N > 1.
    

    For example, factorial(4) would return 24, which is 4 * 3 * 2 * 1 or 1 * 2 * 3 * 4.

  3. Write a function named giggle that will repeat a certain word N times. For example giggle("ha", 2) will return "ha ha" and giggle("ho", 3) will return "ho ho ho". As you can see, this function requires two parameters (arguments).

Getting Help

The TA is available to help you on this assignment between 9-10 PM on Tuesday and Thursday evenings in MCEC 136.