What Are Functions?
Functions are blocks of code that perform a given task any time they are called.
A function could process one action a single time and move on to the next block of code in the script, or it could iterate through itself multiple times (infinitely if you wrote it that way).
In Python, your declare a function with the reserved keyword def
- which defines the function.
Sample function in Python:
def area(length, height):
return length * height
In the simple example above, we define the function called area and give it two inputs, the length and height of an object. Then within the function, the indented text is what the function actually does. In this example it returns the value of length * height.
This example wouldn’t actually produce anything if called by itself. Instead, you’d get an error that the function was expecting two arguments and none were given.
def area(length, height):
return length * height
length = 10
height = 20
rectangle_area = area(length, height) # Notice we are calling the function and giving it variables.
print(rectangle_area)
Now in that example, you would receive a real result if you executed the code in an interpreter.
> > > def area(length, height):
> > > return length \* height
> > >
> > > length = 10
> > > height = 20
> > > rectangle_area = area(length, height)
> > >
> > > print(rectangle_area)
> > > 200