Last time we discussed data types, variables and assignment statements. Today we will take a first look at functions.
How to create a function
Recall, that to open an image we wrote the following code:
>>> filename = pickAFile() >>> picture = makePicture(filename) >>> show(picture)
Let's look at what's going on here:
- We call the function pickAFile, which enables us to select a file on our computer and then returns the full path to that file, which we assign to a variable, filename:
>>> filename = pickAFile() >>> filename 'C:\\Users\\sergey\\Desktop\\sergey.jpg'
- We call the function makePicture passing variable filename as an argument; the function returns a picture object which we assign to a variable, picture:
>>> picture = makePicture(filename) >>> picture Picture, filename C:\Users\sergey\Desktop\sergey.jpg height 251 width 200
- We call the show function, passing the picture object as an argument. The function displays the image in a separate window.
So, we display an image by executing these three statements all together. If we want to display another image, we have to type these three lines again - i.e., we type them for every single picture we want to show. Wouldn't it be nice if we could simplify this by typing just one command, for example pickAndShow? It turns out that's very easy to do!
Consider this. Our goal is to show a picture. We have a process for that; a process that consists of three steps. We know how to execute each step. In other words, we have a recipe for showing a picture. Let's formalize this recipe so that our computer can understand it:
def pickAndShow(): filename = pickAFile() picture = makePicture(filename) show(picture)
We define (keyword def) the word pickAndShow as the name of a our recipe for showing a picture. We can now use this recipe by simply calling it by its name. In other words, we have created a function, pickAndShow, that we can call anytime we want to display an image.
Defining a function in JES
When using JES, define your functions in the program area (not the command area). You can use the command area, but it's less convenient for writing multi-line blocks of code; besides, you cannot save the code you type into the command area - which is what you want to do with your function.
Type in the code for your function definition. Then hit the Load Program button - you will be prompted to save your code as a file - save it with a ".py" filename extension (for example, "myprogram.py"). Then hit the Load Program again - this will load your code into JES, and your function will be available to you - you can now call it from the command area.
Why we need functions
But why go through all this trouble? There are many reasons; here are just a few that are relevant in the context of our class:
- Next time you need to display an image, you DON'T want to remember what code you should write to get the path to the file, then create a picture object, and then display the picture - you should only need to remember the name of your new function: pickAndShow.
- Functions you write later in this class will be more complicated than this example (which is quite trivial). Without a function, not only you would have to remember the code you wrote, you would need to remember the logic of your solution that your code implements - and implement it - without any errors or typos - all over again.
- As you learn how to implement more and more interesting ideas, your programs will get more complicated. Using functions simplifies your code - it becomes much easier to read and understand. Functions help you focus on the big picture and not get lost in implementation details. Recall our exercise on the first day of class: imagine that instead of just "turning left" we would have to plan which muscles, and in what succession we need to activate to accomplish a left turn!
Therefore, whenever you have code that is likely to be reused, you may want to turn that code into a function, and call the function instead of rewriting all the code over and over again.
Using parameters to make our function more useful
Our function is not very useful because it requires us to manually select a file every time we call it. If we wanted to display several files - i.e., all the files in a folder, this would be very annoying. If we knew the filename, we could modify our function:
def pickAndShow(): filename = "C:\Users\sergey\Desktop\0301\sergey.jpg" picture = makePicture(filename) show(picture)
However, now our function works for one file only - which makes it not very useful.
A good function is one that handles the general case - i.e., we want to reuse the same code for displaying ANY image. We can abstract away the filename by passing it as an argument to the function. To do that, we rewrite our function definition, so that now our function can take a parameter:
def pickAndShow(filename): picture = makePicture(filename) show(picture)
Notice that instead of the empty parenthesis, we have the word filename following the function name. This is your parameter, or input variable. When you call the function like this:
>>> pickAndShow("C:\Users\sergey\Desktop\0301\sergey.jpg")- the file path in parentheses is assigned to the input variable filename - which is then used in the body of the function.
Now we can call our function for any image file, and we don't have to rewrite any of its code.
Keep in mind: when you define a function, you specify parameters; when you call a function, you call it with arguments. So:
def printName(name): print name- in the code above, name is the parameter;
>>> printName("Arthur")- in the code above, "Arthur" is the argument.
Functions are essential to what we do in this class (and to programming in general). Make sure you have read and understood Chapter 2 before moving forward. By all means, try to write your own functions - you may be surprised how easy that is, and how much that simplifies your programming tasks!