3.2 Functions
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 to convert a temperature of degree Fahrenheit to degree Celsius. See code below.
F = 70
C = (F-32)*5/9
C
Show expected output
21.11111111111111Example: Make a Python that provides the equivalent degree Celsius for a given temperature in degree Fahrenheit. Use the formula .
- Click
+ Codeto add a code-cell - Type
def temp(F): - Type indent
C = (F-32)*5/9 - Type indent
return C - Click
playbutton
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.
- Click
playbutton ontempcode cell above - Type
temp(100) - Click
playbutton
temp(100)
Show expected output
37.77777777777778Example: Use the temp function above to convert F = 0 to C. See code below.
temp(0)
Show expected output
-17.77777777777778Example: Use the quadratic formula to make a function for solving a quadratic equation of the form:
The solution(s):
If No real solutions
If There is one real solution,
If There are two real solutions
and
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 ,
Here a=1, b=0, c=-4 because
solvequad(1,0,-4) # run the solvequad function above first
Show expected output
The solutions are: -2.0 and 2.0Alternatively, 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.0Run the function solvequad(a,b,c) before you call solvequad(1,0,-4).
Example: Solve
Here
solvequad(3,5,-7)
Show expected output
The solutions are: -2.5733844181517584 and 0.9067177514850918Adapted 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.