Many scientific fields have a basic unit of study:
- Biology has the cell
- Chemistry has atoms and molecules
In Computer Science, our basic unit of study is the function.
You are already familiar with functions from your high school math classes. You probably first encountered them in Algebra I.
For example, in math class, we write:
This defines a function that takes one parameter , and returns the value of . For example, is 9, and if , then is 25.
In Python, we write the same function this way:
def f(x): return x**2
Then we can evaluate
f for various values of
x at the Python prompt:
>>> def f(x): ... return x**2 ... >>> f(3) 9 >>> a=5 >>> f(a) 25 >>>