Working with functions in Python

Desira Wijesundara
8 min readSep 2, 2019

--

Imagine making a cake … with a 7 year old, who is apparently the naughtiest kid around the block with a whole lot of energy, and you are left to babysit this little rascal. You are craving some fresh baked goods, and the kid is complaining about the jelly beans. But wait! The kid is extremely cheeky and won’t let you do any of the work. He wants to bake the cake all by himself. Rules are simple, you give the instructions, and the child follows them. Oh my! how you might wish you hadn’t been born. “Now, now” You say “add half of the butter-” and he has already dropped the whole stick of butter to the mix. You heave a sigh and try to continue, but it’s a mess. He is doing makeup with all purpose flour. He cracks an egg on your new shirt. He sees no purpose in all of this. You decide to take a break.

A kid is definitely hard to work with, although with the correct guidance you might make it to the other side. What if you teach the kid all the kitchen related stuff? poaching, shallow-frying,grilling … no, no, no, that’s unnecessary, and certainly takes too much memory of that little brain. He’ll certainly drop a rock on your head while you’re asleep. But what if you provide only the required skill set before going off into making the cake. Whisking, mixing, folding and baking. That could work, and certainly very sustainable, given that he’ll be making cakes for the rest of his life. With quite a few obstacles, than your previous attempt, you finally bake a cake. The kid is happy, and you are happy(more like relieved). A computer is very much like a child, except it has zero intuition, but to cover that up it has godlike efficiency. You simply cannot tell the computer what to do, and expect it to perform them. If the command you provided is within the limitations of the OS (like, open ‘My computer’) it will work, but if you wanted something done that is not within the boundaries of the OS (like, make a simple Sudoku puzzle game), you have to find a way to talk to the computer directly. The language you use to talk to a computer is called a programming language, and in this article the language incorporated in communicating with the computer is ‘Python’.

The idea in a nutshell

Now let’s take a look back at the introduction of this blog; making a cake with a child. Getting something done in a computer through code alone, is almost as horrendous as making cake with a child, and trust me I know. You have to explain every little detail about the program you are about to write. Importing modules if necessary, assigning values, using operators, and most importantly, using ‘functions’. Now what is a function?

The purpose of a function is for ease of access for the user, for the computer. Introducing a function to a script/code is called ‘defining’ or in language terms ‘def’. A function is like teaching a skill to a child; we assign a certain set of monotonous actions to a single function. What fun is programming if it’s writing the same line over and over again. A function is a reusable code, and can also be described as a set of instructions that can be used inside your program to achieve a specific task.

For example (in relation to the intro); Imagine we are defining the cooking method ‘bake’. It goes as follows,

def bake (Temperature, Duration, Container, Dough)

return cake

Now that you have defined the function called ‘bake’, you can use the this function throughout the code by typing ‘bake’ and assigning values to variables like ‘Temperature’, ‘Duration’, ‘Container’ and ‘Dough’, and the end result will be a perfectly baked cake.

Just like assigning a value to a variable, you assign an action to a function. When writing a programme you firstly introduce the functions which you’ll be using later on. Once you reach the point where you need a function to … well, function (get it?) the piece of code that needs the assistance of a function is named a ‘caller’ and the action of asking assistance from a function is named “calling”. Which translates to a caller calling a function. Once the function is summoned to the piece of code, the code will provide information to the function, which then will process the information and return the information back to the caller.

Defining a basic function

Image 1

In reference to image 1; I have defined a basic function named myfunction which contains a data package; print (“Banana Tiger!”)

Image 2

In reference to image 2; The purpose of the function is to print the ‘string’ called “Banana Tiger” , and it will do just that.

This is as fundamental as it can go in terms of functions, since the print function can only be used to print “Banana Tiger”. You can replace the string with “I do the cha-cha like a sissy girl” but the function would still be printing a string, which is quite restricted and straightforward. We need something flexible enough to complete even more complex actions.

Parameters & Arguments

The meaning of the word “argument ” is unclear if you try to derive it straight on. Think of it this way.

Image 3

Take a look at the code in the above image. This is a simple code that I wrote to define the terms ‘parameter’ and ‘argument’. If you are at least a bit familiar with the whole programming gig, you’ll find out that the functionality of the above code is to add two given numbers, and to spit out the output. If you are unsure you can type this in your IDE and see it’s functionality for yourself.

‘num1’ and ‘num2' are the parameters, while ‘5’ and ‘8’ are the arguments. Although it may sound like an over-simplification, that is pretty much that there is to it. A parameter works as a variable inside the definition of a method, and it receives a value when the method is called. An argument on the other hand is a value that is passed into the methods parameters. In this case, when the method is called the value ‘5’ is passed into the parameter ‘num1’ and value ‘8’ is passed into the parameter ‘num2’.

Types of arguments

Default arguments

If an argument holds a previously assigned default value, and the function is called without the argument, the default value takes its’ place. These type of arguments are called default arguments.

Image 4
Image 5(output)

Consider the above code in Image 4; In the first call, we provide the value for the ‘name’. Now take a look at the first line in Image 5; Apart from the ‘name’ argument we provided(in green), rest of the parameters have taken their previously assigned, if not, default values(in red).

Now if you take the second call in the above code, we have provided a value for the ‘name’ as well as the ‘age’. The output comes in the form of; first two default values being replaced with the provided parameters(in green) and the rest of the parameters taking the default values(in red) as we should expect.

Required arguments

All the arguments assigned to these parameters must be passed in order for the function to work, otherwise it would prompt an error message.

Image 6
Image 7(output)

Since the values are passed during the function call everything seems to be in order, but what if we forget to assign a parameter?

Image 8
Image 9(output)

What does this error mean? It is saying that the function is required one positional argument, and it has even specified that the argument is ‘num’. Simply put, we have not assigned a value to the parameter ‘num’, and this function does not work as long as the specified parameters are associated with arguments.

Keyword arguments

When arguments are assigned to certain Keywords, the keywords can be used in any order while calling the function, and it will not change the corresponding arguments assigned to the parameters. Take a look at the following image below.

Image 10

We have assigned the argument ‘Geralt’ to the parameter ‘name’ and the argument ‘Rivia’ to the parameter ‘origin’, and the output displays as follows.

Image 11(output)

As suggested in the second line of image 10, the function is supposed to print ‘name’ first and ‘origin’ secondly, but what if we decide to switch up the order?

Image 12

The arguments assigned to the parameter remains the same, but the printing order is switched. What can we expect?

Image 13

Even-though the output order is not maintained, the assigned arguments are still the same.

Variable-length arguments

An asterisk is placed before the variable name in-order to indicate that this is a variable-length argument, and is often used in situations where we are not certain of the exact number of arguments that are about to be passed to a function.

Image 14
Image 15(output)

I want to keep this blog within my knowledge range, so this is a wrap. Happy coding!

--

--

Desira Wijesundara
Desira Wijesundara

Written by Desira Wijesundara

Writing is one of the few things I’m actually good at

No responses yet