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 [ ]:
## Code here

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


In [ ]:
## Code here

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 [ ]:
def square(x):
    """
    This function will square x.
    """
    
    return x*x

s = square(5)

print(s)

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?

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?

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?

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 [ ]:
## Code here

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!