Computer Science 15-112, Spring 2012
Class Notes:  Getting Started with Writing Functions


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

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. Some other return types

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

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

    print cubeRoot(8)
    print isPositive(8)
    print isPositive(-8)

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


     

  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()
  10. Examples and Practice
    1. Annotated Examples for Self-Study on Writing Functions
    2. More Practice on Writing Functions

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