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.
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 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 and 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 5, 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 going 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 gonna hurt, right?
We're typing so much code that's repeated because we don't have a way of bundling all 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.
Now if you click on Reebok's keyboard, you can see all of the functions that we can use to command
our robot like move() or turn_left(), or build_wall, etc..
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 to 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 this 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 wanted to do that many times, say for example, if we wanted to go to 3, turn around,
go back to 1, turn around, go back to 3,
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 3, turn_left(), turn_left(), and then move two
times again to get to 1 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.
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 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 is 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 turn_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.
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.
All right so let's go from the beginning.
We start out facing right at the position 1, 1.
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 write 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 at 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.