functions for coursera programming assignment 2