The first concept I want to introduce you to is something called functions.
And just as we perform many different functions could be mother, father,
son, daughter,
we can also get our code to perform different pieces of functionality.
Now, if we think back,
we've actually already come across functions and I've been referring to them
every time we've needed to use one.
If you take a look at this link in the course resources
which takes you to the Python documentation,
then you can see that Python has a whole bunch of built in functions that we've
been using. For example, the len function,
which gives us the number of items in a collection or the int function
which turns something into an integer or the print function,
the range function, and other ones that we haven't seen yet.
If you head over to the Day 6
starting Repl.it and go ahead and forward a copy of it.
Then we can start typing one of the functions that we've been using a lot,
which is the print function.
And the reason why we know it's a function is because it's the name of a
function followed by a set of parentheses.
Now we know that if we put something inside these parentheses,
say, for example, hello, then when I run the code,
whatever is inside those parentheses
will get outputted over here in the console.
Now all of the functions work pretty much the same.
So if I wanted to use the len function to see, well,
how many characters are there in this word Hello? I, again,
put my hello inside the parentheses because of the parentheses
we know that this is a function and then I can save the outcome of this length
function to a variable, say,
let's call it number of characters, num_char.
And then I can go ahead and print it in order to view it inside the console.
So let's print the num_char,
and now you can see that this len function gets to work, calculates the
number of characters, it's five, and then it gets printed.
So all of this functionality has been achieved by these built-in functions from
Python, like print and len.
Now what if we wanted to make our own functions?
How would we do that? Well, if we want to make our own function,
we first start out with a keyword,
which is called def.
And this is because we're creating or defining our function.
Now, after the def keyword,
we can give our function a name and I'm just going to call it my_function.
Now, the thing that differentiates a function from a variable, however,
is the parentheses. So after the name comes the parentheses,
and now the final thing,
the finishing touch to our function definition is a colon because that says
everything that comes after that line and is indented belongs with the
function.
Let's make a really simple function that just has two lines of code.
Maybe it prints Hello and then it prints Bye.
So now I've created my function,
I've placed the lines of code that are associated with this function all indented
after the definition. But notice how,
if I go ahead and run my code, actually nothing will happen.
And the reason is because we haven't yet executed the function.
Now we can define lots and lots of functions ahead of time.
And it's only when we actually need it
do we go ahead and trigger it and to trigger it or in programming lingo,
we would say to call the function,
all we have to do is type the name of the function,
which is my_function
and then to add the parentheses and any necessary inputs. In our case,
our function doesn't require any inputs so we can leave the parentheses blank
again. And now if I go ahead and run my code once more,
then you'll see that by the time the computer reaches line 5,
it's going to search for this thing called my_ function.
It sees that it was defined right here on line 1
and it goes through all of the content and executes them in turn, line by line.
And that's why we get hello and bye being printed here.
So just to recap, this is how we create a function in Python.
There's two steps to it. The first step is to actually define the function,
to specify what it should do.
And we do that by first using the def keyword and then we give our function a
name. So for example, in this case,
I called it my_function and then comes a set of parentheses and a colon.
And then after that, we get to put the lines of code
which will be included in this function. And remember that these lines of code
which goes into the function
which will be carried out when this function is triggered
must be indented like this.
So once you've defined the function, you've created the recipe,
the next step is to actually use it, which in programming lingo,
we would say calling the function. And we call the function just by specifying
the name and a set of parentheses.
And once the computer sees this line of code it'll know to go and carry out all
the instructions inside
where we defined our function. And to learn about functions,
I'm to introduce you to something that's very similar to Karel the robot.
The robot is going to be something that is just going to perform the tasks that
we want it to, and no more and no less.
So for example,
if we wanted a robot to go to the store and pick up some milk for us,
we can't simply just tell it, Oh yeah,
just go and buy some milk because it won't know how to do that
unless
we give it specific instructions such as leave the house,
walk two blocks to the right, walk four blocks back and then two blocks to the right.
When you go to the store,
give them some money and then take the milk and come back.
We have to program each and every step. But say,
if this functionality of getting the robot to go and pick up some milk for us is
needed every single day
then we have to write out all of those instructions day after day,
day after day. And at some point our fingers are going to hurt, right?
We're typing so much code that's repeated because we don't have a way of bundling
all of those instructions together. And that's where functions come in.
Functions will give us a way of referring to all those instructions
at the same time.
That way we can give our robot a single instruction and it will carry out all of
those little steps for us and get us some milk.
Now to try this out yourself,
I want you to head over to the course resources and click on the link that takes
you to Reeborg's World.
Now Reeborg's World is very similar to Karel the robot that I showed you
earlier on,
but it allows us to write Python code. And it's very easy to get started.
Now we've got this area here which we're going to use to write our Python code.
And then once we're done, we can click on play to
run the Python code and see the instructions carried out by our robot.
But if you click on Reeborg's keyboard,
you can see all of the functions that we can use to command our robot,
like move or turn left, or build a wall, et cetera.
Let's see if we go ahead and run this line of code here, move.
What happens? When I click play
you can see that our robot moves forward in the direction that it's facing by
one step. So if we wanted it to move, say for example, three steps,
all we have to do is the call the move function three times.
Now let's reset by clicking the return button to return our robot back to the
starting position.
Now let's click play again in order for our robot to carry out the new
instructions.
Notice how all the instructions were carried out very quickly. To execute each
instruction one step at a time, line by line,
we can use this step-through button.
This button allows us to see how each instruction is executed and after each
instruction, it pauses and highlights the next instruction.
This is very helpful for debugging.
So notice how we're highlighting line 2. So if I click on step again,
it's going to carry out that function and then it's going to carry out line
3 and finally it reaches the end.
And that's the end of our code. Now,
in addition, we can use some of the other commands in the keyboard
for example, turn left.
If I decide to say move three times and then turn left and then maybe move three
times again, can you predict which square the robot is going to end up in?
Are you ready? Let's go ahead and run the code. There we go.
We end up on the square 4,4.
It moves three forwards turns left and then moves three steps again,
because that's what we told it to do with our Python code.
So now here's a question. If you take a look inside the set of commands,
you'll notice that there is move and there is turn left. But there is no turn
right and there is no turn around.
So how could we use a function to create those commands?
Because, of course, we could just simply write,
turn left and then turn left again.
And this would effectively turn the robot around, right?
But if we want to do that many times, say for example,
if we wanted to go to three, turn around, go back to one, turn around,
go back to three, then we would have to write code that looks a bit like this.
We would have to say move
two times to get to square three, turn left, turn left,
and then move two times again to get to one and then turn left,
turn left again in order to turn around.
This is all the code we would have to write to achieve this kind of effect.
But instead of all of this, we could just define a new function.
So we use the def keyword to say that this code is defining a new function
called turn_around
and this function is simply going to be turn left twice. And remember, all the
code that's going to go inside the function has to be indented.
And now instead of writing, turn left, turn left,
we can simplify our code by telling it to call the function,
turn_around, and we can do that here and we can do that here.
So now when we run the code,
you'll see that it does exactly the same thing as before,
but this time we've cut down two lines of code.
So instead of eight lines of code for our instructions,
we only now need six lines.
And you can see that the more that you need to use this function,
the more typing it will save you. But more important than reducing
the number of lines of code is that our code has also become a lot more
readable.
So the turn_around function is much more clear than having turn left twice.
Now let's see what happens when we use the step through button.
Look closely at how each line in our code is executed.
So now what we're going to carry out the move by one, move by one
and now it's going to call turn_around, that function. And it jumps over to find
this function and carries out the code inside in turn from the top to the
bottom. So turn left, turn left. And now once that function's done,
it goes back to where it was before and continues running, move, move,
and then turn around again.
So here's your challenge.
Try and create a separate function that turns our robot to the right.
Pause the video,
head over to the course resources and click on the link that takes you to
Reeborg's World
and then go ahead and create your own function called a turn_right.
Right.
All right.
So this should be pretty simple because all we have to do is to define a new
function called turn_right
and this function is going to simply be turn left three times
Right?
because turning left three times is going to effectively turn our robot
right.
So even though we only have this one way of turning our robot,
we can define our own functions to give it many,
many other commands that it could use.
So now using this turn right function,
see if you can write some code that makes our robot draw a little square going
from here to here then to here and back to the beginning.
Once you're done and you hit play, this is what should happen.
So pause the video now and see if you can complete that challenge.
Right?
All right. So let's go from the beginning.
We start out facing right at the position 1,1
one in order to draw the square, starting from this way,
we have to first turn left
and then we have to move one step and that should take us to here.
And now we have to turn right, move
one more step, turn right again,
move one more step and finally turn right for the last time and move one more
step.
So using our awesome function means that we don't have to right out turn left
three times each time we need to turn right.
So our code can just be eight lines long instead of 14 lines long
and we're using our function to cut down on a lot of typing.
But not only are there fewer lines of code,
look how much more readable our code has become thanks to our turn right
function. It's now so much easier to read, turn right,
and understand what it needs to do rather than seeing turn left three times in
the code.
So now it's easier to read and it's easier to follow the logic in our code.
In the next lesson,
I've got a challenge for you. Head over there and apply what you've learned
about functions to try and solve it. I'll see you there.