Functions

Making reusable blocks of code.

Starting point:

In this exercise, we're going to calculate one of the roots from the quadratic formula:

$r_{p} = \frac{-b + \sqrt{b^{2} - 4ac}}{2a}$

Determine $r_{p}$ for $a = 1$, $b=4$, and $c=3$.


In [2]:
## Code here
import math

(-4 + math.sqrt(4**2 - 4*1*3))/(2*1)


Out[2]:
-1.0

What about for $a = 2$, $b = 8$, and $c = 1$?


In [7]:
## Code here 

# gonna make this saner
a = 2
b = 8
c = 1

(-b + math.sqrt(b**2 - 4*a*c))/(2*a)


Out[7]:
-0.12917130661302934

Functions:

  • Code can be organized into functions.
  • Functions allow you to wrap a piece of code and use it over and over.
  • Makes code reusable
  • Avoid having to re-type the same code (each time, maybe making an error)

Observe how this function works.


In [5]:
def square(x):
    """
    This function will square x.
    """
    
    return x*x

s = square(5)

help(square)


Help on function square in module __main__:

square(x)
    This function will square x.

Observe how this function works.


In [ ]:
import math

def hypotenuse(y,theta):
    """
    Return a hypotenuse given y and theta in radians.
    """
    
    return math.sin(theta)*y
    

h = hypotenuse(1,math.pi/2)

print(h)

Summarize:

What is the syntax for defining a function?

def FUNCTION_NAME(ARGUMENT_1,ARGUMENT_2,...,ARGUMENT_N):
    """
    Description of function.
    """

    do_stuff
    do_other_stuff
    do_more_stuff

    return VALUE_1, VALUE_2, ... VALUE_N

How do you get information into a function?


In [ ]:
def some_function(ARGUMENT):
    """
    Print out ARGUMENT.
    """
    return 1
    
    print(ARGUMENT)
    
some_function(10)
some_function("test")

Summarize

How do you get information into the function?

You get information into the function via arguments defined in the function name.

Modify

Alter the code below so it takes two arguments (a and b) and prints out both of them.


In [ ]:
def some_function(a):
    """
    print out a
    """
    print(a)

In [ ]:
def some_function(a,b):
    """
    print out a and b
    """
    print(a,b)

Predict

What does b=5 let you do?


In [ ]:
def some_function(a,b=5,c=7):
    """
    Print a and b.
    """
    
    print(a,b,c)
    
some_function(1,c=2)
some_function(1,2)
some_function(a=5,b=4)

b=5 allows you to define a default value for the argument.

How do you get information out of a function?


In [ ]:
def some_function(a):
    """
    Multiply a by 5.
    """
    
    return a*5

print(some_function(2))
print(some_function(80.5))

x = some_function(5)
print(x)

Summarize

How do you get information out of the function?

By putting return at the end of a function, you can capture output.

Modify

Alter the program below so it returns the calculated value.


In [ ]:
def some_function(a,b):
    """
    Sum up a and b.
    """
    
    v = a + b
    
    return v

v = some_function(1,2)

print(v)

In [ ]:
def some_function(a,b):
    """
    Sum up a and b.
    """
    
    v = a + b
    
    return v

To return multiple values, use commas:


In [ ]:
def some_function(a):
    """
    Multiply a by 5 and 2.
    """
    
    return a*5, a*2

x, y = some_function(5)

print(x)

Implement

Write a function that uses the quadratic equation to find both roots of a polynomial for any $a$, $b$, and $c$.


In [8]:
## Code here

def get_root(a,b,c):

    return (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

print(get_root(1,4,3))
print(get_root(2,8,1))


-1.0
-0.12917130661302934

Summary

  • Functions are defined by:
def FUNCTION_NAME(ARGUMENT_1,ARGUMENT_2,...,ARGUMENT_N):
    """
    Description of function.
    """

    do_stuff
    do_other_stuff
    do_more_stuff

    return VALUE_1, VALUE_2, ... VALUE_N

Rule of thumb: NEVER copy a block of code in the same program!