Odin May | Google Colab Notebook
This is a short description for the post which introduces it
Functions are another fundamental part of programming. Lets say you want to print something out to the console, well luckily there is already a function for that. The print function is one of the most commonly used build in functions. It can seem very basic but can be used for a variety of things such as debugging, printing graphics and information to the terminal and many other things. Python comes with some built in functions for us to use out of the box, such as the print function but we can define our own functions.
A function can be thought of as a set of instructions that can be called to run. Lets take a look at an example, say we want to define a function that prints out a message. We can write it as follows
def greet():
print(‘Hello’)
greet()
Lets take a closer look at the code above. Notice how it reads very similar to what we want to do. We start by defining our function called ‘greet’ and then we use parenthesis and a colon. On the following line we use Tab to indent, in this indented area we write the code that we want to be executed when ‘greet’ is called. To call the greet function we just type the name of it with parenthesis, this will call the function to execute its code.
There’s a few things to keep in mind when working with functions. We can pass in information that can be used within the function if we would like, these are called arguments. When we define the function we don’t know exactly what will be passed into it so we are simply naming a variable that will be used as a placeholder for whatever is passed in.
def greet(name): # name is our variable
print(‘Hello ’ + name) # here we use the name variable
greet(‘Odin’)
Now that our function expects an argument, when we call it we have to pass it in when we call it. A function can have multiple arguments as well, when you use multiple arguments, you have to pass them into the function in order.
def greet_both(name1, name2):
print(‘hello ‘ + name1)
print(‘hello ‘ + name2)
greet_both(‘Odin, ‘Bill’)
These arguments are called positional arguments. We choose the name and expect a value to be passed in and they are set to variables in order. In the example, name1 will be set to ‘Odin’ and name2 will be set to ‘Bill’. We can use keyword arguments to set a default value in the case we don’t get a variable passed in or when we have optional arguments. To make a positional argument into a keyword argument we just assign it a default value, simple as that!
def greet(name=’Human’):
print(‘hello ‘ + name)
greet()
Functions like the ones we wrote here are just calling the print function which is returning a string of text to the console. Lets try defining a function called power_of_two that takes a number and returns that number to the power of 2.If no argument is passed in, lets just make the default 3. Note that this function is not printing anything its just returning a value, I will explain more after the example.
def power_of_two(number=3):
return number * number
power_of_two(4)
Okay so this function give us no output, what’s up with that. Well we aren’t calling a print function within it, but we are returning a value. So when this function gets evaluated the result can be printed if we want, or stored in a variable like this
result = power_of_two(4)
print(power_of_two(5))
print(result)
Python evaluates the function and whatever value is returned is used in its place. A lot of functions return values instead of just printing information because it is much more versatile. I recommend looking at the built in functions and getting familiar with them here. These are functions that are prepackaged with Python and will come in handy quite often.
A key principle in programming is Don’t Repeat Yourself. This is commonly referred to as the DRY principle. If you are ever writing code and see that you are repeating yourself, there’s almost always a better way to do it that will save you from writing the same thing over and over again. Functions are usually the answer, sometimes there is a more elegant solution. If you would like to see more examples of functions and get some ideas of ones you could write yourself for practice check out this post.