Login
📚 Python for Introductory Statistics
Chapters ▾

3.2 Functions

Watch demo video

A function is a reusable piece of code. It is a block of code which only runs when it is called. You can pass data, known as arguments, into a function. A function can return data as a result. In Python a function is defined using the def keyword. This is similar to built-in functions such as abs or print but "your own".

Syntax

def func_name(arguments):
    block statements
    return result # optional

def is a keyword in Python. It is used to define a function.

Example: Use the formula C=(F32)59 to convert a temperature of F=70 degree Fahrenheit to C degree Celsius. See code below.

F = 70
C = (F-32)*5/9
C
Show expected output
21.11111111111111

Example: Make a Python that provides the equivalent degree Celsius for a given temperature in degree Fahrenheit. Use the formula C=(F32)59.

  1. Click + Code to add a code-cell
  2. Type def temp(F):
  3. Type indent C = (F-32)*5/9
  4. Type indent return C
  5. Click play button

Note: No output is displayed for function. It will display output when it is called with specific argument is provided. See example below.

def temp(F):
  C = (F-32)*5/9
  return C

Note: the name of this function is chosen to be temp. There is one argument 'F'. The indented body of the function uses the formula to compute C. The return line displays the value of C.

Example: Use the function temp(F) above to convert a 100 degree Fahrenheit to Celsius.

  1. Click play button on temp code cell above
  2. Type temp(100)
  3. Click play button
temp(100)
Show expected output
37.77777777777778

Example: Use the temp function above to convert F = 0 to C. See code below.

temp(0)
Show expected output
-17.77777777777778

Example: Use the quadratic formula to make a function for solving a quadratic equation of the form:

a x 2 + b x + c = 0

The solution(s):

If b24ac<0 No real solutions

If b24ac=0 There is one real solution, x=b2a

If b24ac>0 There are two real solutions

x1=bb24ac2a and x2=b+b24ac2a

See code below:

# quadratic equation solver
def solvequad(a,b,c): # the name is solvequad and arguments a,b,c in that order
  a = float(a)              # converts or casts to float
  b = float(b)
  c = float(c)
  if b**2-4*a*c < 0:
    return print("No real solutions")
  elif b**2-4*a*c == 0:
    x = -b/(2*a)
    return print("The solution is", x)
  else:
    x1 = (-b-(b**2-4*a*c)**.5)/(2*a)
    x2 = (-b+(b**2-4*a*c)**.5)/(2*a)
    return print("The solutions are: ",x1," and ",x2)

Example: Solve x24=0,

Here a=1, b=0, c=-4 because 1x2+0x+(4)=0

solvequad(1,0,-4)     # run the solvequad function above first
Show expected output
The solutions are:  -2.0  and  2.0

Alternatively, you can pass a=1,b=0,c=-4 to the function as follows:

solvequad(a=1,b=0,c=-4)     # run the solvequad function above first
Show expected output
The solutions are:  -2.0  and  2.0

Run the function solvequad(a,b,c) before you call solvequad(1,0,-4).

Example: Solve 3x2+5x7

Here a=3,b=5,c=7

solvequad(3,5,-7)
Show expected output
The solutions are:  -2.5733844181517584  and  0.9067177514850918

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.