Computer Science 15-110, Spring 2011
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

Writing Functions

  1. functions with no parameters

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

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

    def printSquare(x):
        print x, "**2 =", (x*x)

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

    def printSum(x,y):
        print x, "+", y, "=", 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 x, "**2 =", square(x)

    printSquare(3)
    printSquare(4)

    Another example:

    def square(x):
        return x**2

    def squareRoot(x):
        return x**0.5

    def hypotenuse(a, b):
        return squareRoot(square(a) + square(b))

    a = 3
    b = 4
    c = hypotenuse(a, b)
    print "hypotenuse =", c

  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:

    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))


    Yet another example:

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

    print initials("Douglas", "Adams")
     

  8. Parameter and Local variable scope

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

    x = 5
    print "first, x =", x
    result = addOne(x)
    print "calling addOne(x) returned:", result
    print "and now x =", x


    Another example / Globals (do not do this!)

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

    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()


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