Computer Science 15-110, Fall 2010
Class Notes:  Writing Functions


  1. functions with no parameters
  2. functions with one parameter
  3. functions with multiple parameters
  4. int functions (Fuctions that return a value)
  5. Functions calling other methods
  6. Other return types (float, boolean, string)
  7. Local variables
  8. Parameter and Local variable scope
  9. Test functions
  10. Practice

Writing Functions

  1. functions with no parameters

    def doSomething():
        print("Carpe diem")

    doSomething()
    doSomething()
     
  2. functions with one parameter

    def printSquare(x):
        print(str(x) + "**2 = " + str(x*x))

    printSquare(2)
    printSquare(3)
     
  3. functions with multiple parameters

    def printSum(x,y):
        print(str(x) + " + " + str(y) + " = " + str(x+y))

    printSum(2,3)
    printSum(3,4)

     
  4. int functions (Functions that return a value)

    def square(x):
        return x*x

    print square(3)
    print square(4)
    x = square(3) + square(4)
    print x
     
  5. Functions calling other functions

    def square(x):
        return x*x

    def printSquare(x):
        print(str(x) + "**2 = " + str(square(x)))

    printSquare(3)
    printSquare(4)
     
  6. Other return types (float, boolean, string)

    def cubeRoot(d):
        return d ** (1.0/3.0)

    def isPositive(x):
        return (x > 0)

    def firstChar(s):
        return s[0]

    def initials(firstName, lastName):
        return firstChar(firstName) + firstChar(lastName)

    print cubeRoot(8)
    print isPositive(8)
    print isPositive(-8)
    print firstChar("ABCD")
    print initials("Douglas", "Adams")
    print firstChar(initials("Douglas", "Adams"))
     
  7. Local variables
    def minSquared(x,y):
    smaller = min(x,y)
    return smaller*smaller

    print minSquared(3,4)
    print minSquared(4,3)

    Another example:

    # This function computes the area in the first quadrant under
    # the line y=mx+b.  It assumes m is negative and b is positive,
    # so we in fact have a triangle in the first quadrant.
    def areaUnderLine(m, b):
        # The x-intercept of y=mx+b is where y=0, so mx+b=0, so x=-b/m
        width = -b/m
        # b is the y-intercept
        height = b
        # now we have the width and height of the right triangle
        # under the line in the first quadrant, so...
        return width*height/2

    print "Area under y=-2x+8 is: " + str(areaUnderLine(-2,8))


    Yet another example:

    def isEvenPositive(x):
        isEven = ((x % 2) == 0)
        isPositive = (x > 0)
        return (isEven and isPositive)

    print(isEvenPositive(-2))
    print(isEvenPositive(-1))
    print(isEvenPositive(0))
    print(isEvenPositive(1))
    print(isEvenPositive(2))


    And yet another example:

    def initials(firstName, lastName):
        firstInitial = firstName[0]
        lastInitial = lastName[0]
        return firstInitial + lastInitial

    print initials("Donald", "Knuth")
     

  8. Parameter and Local variable scope

    def addOne(x):
        x = x + 1
        print "in addOne, x = " + str(x)
        return x

    x = 5
    print "first, x = " + str(x)
    print addOne(x)
    print "and now x = " + str(x)


    Another example:

    def printN():
        print n   # n not local -- so it is global (bad idea!!!)

    printN()  # to make this "work", comment out this line
    n = 5
    printN()

     
  9. Test functions

    def minSquared(x,y):
        smaller = min(x,y)
        return smaller*smaller

    def testMinSquared():
        print "Testing minSquared... ",
        assert(minSquared(2,3) == 4)
        assert(minSquared(3,2) == 4)
        print "Passed all tests!"

    testMinSquared()
  10. Practice
    See http://kosbie.net/cmu/spring-10/15-110/handouts/quiz1-practice.html#Console_Programs

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem