Functions (Constant)
Let's learn functions! Functions are incredibly useful and one of the best parts of programming, as such, we will use them through the remainder of the course. This material is a little more dense than previous. In addition, throughout we will include some more advanced use cases in hopes of teaching good coding practices right from the get-go, so that it will become a habit later on.
What is a function? Here's our definition:
A function is a self-contained block of code that encapsulates a set of instructions. It provides an abstraction over these instructions, promoting modularity and reusability. Typically, functions can accept input parameters and return an output, allowing them to be flexible and widely applicable across different scenarios.
There is a lot to unpack there! We'll go over in detail the new terminology in this definition across the next few lessons. By the end of this section on functions, this definition should make more sense. To start, let's look at how we can write the most bare-bones function we can in Python.
def constant_function():
return 0
Let's understand this piece of code step by step:
- def: This is a special word in Python that means we're about to create a function. Think of a function like a small machine - we give it some input, it does something, and then it gives us some output.
- constant_function: This is the name we've given to our function. Just like you have a name, we name functions so we can use (or 'call') them later.
- (): The empty parentheses means our function doesn't need
any special information to work. Sometimes, functions need extra
details (called 'parameters') to do their job. The colon
:
at the end tells Python that we're about to write the instructions for our function. - Inside the function, we see
return 0
. The word return is another special word in Python. It's like the output of our machine. In this case, our function always gives back the number 0, whenever it's used.
So, in short, every time we use (or 'call') our
constant_function
, it will always give us back
the number 0.
Function names
Python has a few expectations about function names. First, it can contain
any alpha-numeric character as well as underscores. Other characters such as a
pound sign #
or an exclamation point !
will confuse
the interpreter. Functions are often named something descriptive of what it does.
For example print_number
and bake_cookies
would be clear names describing the purpose of the function. Whereas a name like
fabulous_function_009
might be fun to write,
but doesn't tell us what it actually does!
The return of a function
We saw earlier the function return the value 0, but we may write
functions that do not return anything at all. For example, we might
have a function that opens a file, writes something into it, then closes
it. Or, in another example, it could return an indication of a
successful outcome. If there is no
return statement at the end of the function, then the Python compiler marks
the end of the function when the indentation goes to the same level as the
def
. For this reason, white space is very important in Python.
In the next code editor, since we do not include the return
statement, the interpreter will automatically add - behind the scenes for us -
the line return null
at the end of the function.
def constant_function():
x = 1