Sitemap

Functions — Python 101

4 min readJan 24, 2018

--

So, far, we’ve talked about containers and variables. Today, I want to talk about functions. I am assuming that you know absolutely nothing about programming. To really understand functions in Python, you’ll need to have a solid understanding of why we use them, what arguments are and what the overall cycle of a function looks.

What is a Function? It is an operation.

A function is basically a way of organising instructions. Think of a thing that you do regularly. Having experience means that you know what do, right? How? Well, you’ve got a clear idea in your head of what the steps necessary for that thing are. Functions are basically like that. They’re things you can write once that stop you from needing to repeat the same bit of code in loads of places. Once you’ve written a bit of logic down, you can just refer back to it, and be like, yo, do that thing again.

For example, we can think of addition as a type of function. You can add any 2, 3, 4… numbers together. Or cooking, or cleaning, or commuting. These are a series of steps squished together and “encapsulated” under a single name. In fact, think of any operation that needs a set of steps that can be defined as a function. Kind of feel like our brains organise stuff in this way too.

What are arguments or parameters? They’re the entities involved in a function.

When you want any task done, you can think of it in terms of two things: 1. The logic of what needs to be done and 2. The things/people involved in that operation — that’s basically what an argument is. It’s an entity that is somehow involved in the way an operation gets done. So, we can think of cleaning as a function (read: operation) and Abdul as an argument. The cleaning operation can also accept more than one argument, right? It can accept (done by/done to/done with) lots of different people/things/stuff. So even though the logic of cleaning is still the same, the arguments are different, which means that the overall function will look different. Don’t worry if you still don’t know what an argument is still, we’ll go through some examples together.

Four Things I need to do for every function

First: you need to define it. Use the def statement to create the function object and assign a name to it. When you’re defining a function, what you’re doing is describing the inner working logic of the operation. So, how does I tell a computer what an operation is? So,

def cleaning():

says, hey computer, I want you to create a function object called cleaning. Enclosed in this function definition is the set of instructions and who/what they will be done by (arguments).

Second: you need to decide on what the arguments will be for your function. Parameters or arguments are the entities involved in your operation. So, if I say, yo go and add 3 and 4, there are two arguments(3 and 4). So,

def cleaning(a, b):

is saying, hey computer, create a function object with two arguments. Writing it in this way means that I don’t need to repeat my code. Because a can just stand for Mary and b stand for George. Or a in another example can stand for Jill and b for Jack. By requiring two arguments, I’m saying this is an action/operation will need two things/stuff/variables.

Get AP Jama’s stories in your inbox

Join Medium for free to get updates from this writer.

third: return stuff. What do you want the take away from your operation to be? The return statement basically says, hey, here is the result for your thing. We’ll return to the return statement in the next functions piece when you we talk about scope and namespaces(I said return return lol). But for now, if it was addition example, here is how we might use the return statement:

def addition(a, b): → here, we’re like defining the function.

return a * b → here, we’re saying, gimme a times b as a result

Finally: you call the function. This is the bit where the actual operation happens. In our maths example, this is the moment it does the number crunching. Or in the cleaning example, this is the moment that actual cleaning gets done. All that we have done up to this point is describe what cleaning might look like and who might do it. To call a function, just say

cleaning(jack,jill)

where jack and jill are the two arguments involved in the operation cleaning. This will execute on run time.

addition(6, 12)

here, I’m actually calling the function ie. do the damn thing my g. And it gives me the result of a*b where a = 6 and b =12. The answer of course is 72 and that is what is returned to us.

Examples of Functions and how to read them

def justice(league): → this is a function called justice, it has 1 argument. We can’t see the body of the function, all we can see is the def statement and arguments.

def func1(arg1, arg2, arg3): → this is a function called func1 with 3 arguments. To see how these three arguments interact, i have to look inside the function.

def chores(child_1, child_2, child_3): → this is a function called chores, with three arguments. I don’t really know the role played by child_1, child_2 or child_3 but I do know that they all play a role in the function called chores.

Press enter or click to view image in full size

This is a real function taken from the Tensorflow library, a machine learning library in Python. Here, we’ve a function called “train_input_fn”. It has three arguments. These are called features, labels, batch_sizes. We don’t know anything about those variables but we know that they’ll all play a role in this function!

Question: what does it return?

Now you’re ready to watch this video

This guy is dope. I learnt a lot of what I know about Python from him. Watch the video and you’ll be great.

As always, hit me up if you have any questions.

--

--