One of the most important things you want to do when programming is to avoid repeating code too much.
So let's consider this program.
Here I have first.
So I declare a variable a is equal to two.
And then I have three more variables.
And for each one I actually want to do the triple of number a.
So that's going to work.
But instead of repeating the same code to triple the number, every time that we need to triple the
number, let's create a function that does just that.
So what is a function?
A function is a reusable block of code that you can call from another part of your program.
A function has a name, and it can take some input parameters and also can return some values if you
want to.
Let's create one function here so you will understand.
And I'm going to do that at the beginning of the program here.
So I'm just going to put a few more lines and let's create it there.
So there is a specific syntax that you will use to create any function.
The first thing to write is the keyword def.
You can see we have a syntax highlighting for def.
Okay so you write def and then space and you rewrite the name of the function.
And best practice here is to use a meaningful name so that when somebody or you in the future read your
function it makes sense.
Okay.
So we want to triple a number.
Let's just call our function triple number.
Okay.
As simple as that.
And as you can see if we want to have several words inside the function no problem.
You just add an underscore okay.
No space just an underscore.
Then after the name of the function you will open and close parentheses and finish with a colon.
Now in those parentheses you can add input parameters.
Okay.
So when we call the function we can send some data to the function.
And the function can process this data.
So for example numbers well any variable you want.
And let's call this parameter here Number.
Okay.
So we create here a number as an input parameter.
You can have zero parameter.
You can have one parameter.
You can have as many parameters as you want.
You just separate them with a comma.
Okay.
So here for example I have three parameters.
Let's just keep one.
And then you will press.
So after the colon here you press enter.
And you can see that when we go back to a new line we don't go to the beginning of the line.
We go to.
You can see here we have four spaces.
So we have an indentation of four spaces.
And this indentation is very important.
I'm going to come back to this a bit later.
And here.
So just after this line with this indentation we can start to write the code for the function.
So you can write some code.
And we can use the keyword return.
So we return you finish the function and you return a value.
What do we want to return.
Well we have the number parameter here.
We want to return number.
Multiply by three.
Okay.
And that's it for the function.
So we write def then the name of the function we open and close parentheses.
We can add some input parameters.
Then a colon we go back to a new line with an indentation.
We can write some code.
You can write code on several lines if you want to.
And then you may return something.
So if it's the case you just use the return keyword with some value here.
And then after we have defined the function okay.
So def is for define, then you can use it in the code.
So instead of writing this here I can just do triple number and then pass a as an input parameter okay.
So what will happen here is that I'm going to call this triple number function that's defined here.
And at this point I'm going to pass a.
So not a but actually the value inside a okay.
So when I do this I'm going to call triple number with the value two okay is going to be received as
the number parameter.
So the number variable inside this function.
And then it's going to return the number.
So two times three is going to return six.
So we have six as a result here.
And let's just do the same.
So let's just copy this one and put it there as well and there as well.
And let's just because it's not going to print anything let's just print B for example okay.
We print this variable here and you can see the result is six as I just explained.
So that's how you create a function.
And now you can do whatever you want here.
For example we could say d is equal to triple number a and then plus triple number of c for example.
Let's see what will happen.
So I'm going to print D instead.
And this time we have the value 24 Okay.
Why?
24.
Because you can see we first call triple number A.
Okay.
So that will return six as we just saw previously.
And then triple number C what's the value of C.
Well the value of c is six because we have the triple of two.
So the value of c is six.
Here we pass six as a parameter to this function.
And then it returns six times three.
So it will return 18.
Okay.
So 18 plus six.
We have 24.
All right.
And you have seen how to create a function that returns a value.
But let's just create another function.
So I'm going to I'm going to go back to a new line.
You can see I go back to the previous indentation I go back to new line.
Let's add some space for readability.
And I'm going to create a new function that doesn't return anything.
So I use def and let's say I want a function that prints the triple number.
So let's just call it print Triple number.
Okay.
So instead of actually calling the function and then using print, we can do all of that with just one
function.
And I'm going to create an input parameter named number.
I close the parentheses I add a colon.
I go to a new line with enter okay new indentation.
And how can I print the triple number.
Well let's create a variable here.
Result is equal to triple number and I will pass the number.
Okay.
So when I get to this print triple number function I receive a parameter called number.
And I call the triple number function.
So I call another function from this function.
And I pass the value of number.
Okay.
This is going to go into this other number parameter.
And it's going to return the number times three.
So we get the result here.
And then I can print.
So you can see I can just press enter again and print the result.
Okay.
And we stay.
You can see in the same indentation so that we are still in the function.
So now I can call this function I'm just going to remove that.
And let's say print triple number.
And note here you can use Ctrl and space.
And you will see this kind of stuff.
And then you can press enter.
Ctrl space is basically the Autocompletion tool okay.
In programming you will see you will use that a lot.
Autocompletion.
You can start a variable name, a function name for example, and then press Ctrl Space Anthony and
you will get suggestions about how to finish your variable or your function.
And as you progress with programming, you will use that a lot, because what it will make you gain
a lot of time and also you will make less errors because the name for sure will be correct.
Okay, you don't have any risk of making a typo if you use Autocompletion.
All right.
So I'm going to do print triple number and let's give five.
Okay.
Just like that.
So to call a function, you can see that.
Yeah we have to call.
So we have to write the name of the function.
And then open and close parentheses and provide the required amount of parameters.
We just have one.
But if you had several parameters you would need to add a comma between each parameter.
Let's run that.
And you can see we have 15.
So in this case we go.
So we call print number.
We go here five goes here.
Then five goes to triple number function okay.
Multiply by three.
We return it inside the result variable.
And then we print the result variable okay.
You can see the flow of execution here.
And one thing I can improve here is well I just create a variable and I print it.
But I don't really use that that much.
So I can just instead do print.
I open the parentheses and I close the parentheses here.
And I'm going to remove this line.
So you can see here I use a function okay inside a function.
So the result of triple number is going to be used for the print function.
All right.
So you can combine functions like this.
And let's finish with a third function I'm just going to call it say hello.
This function just says hello.
And I'm not going to pass any parameter okay.
So you can see def then say hello open close parentheses colon.
Go to a new line print just hello.
All right.
And that's a very simple and basic function I can call it here.
Say hello okay I need to open and close parentheses always to call the function.
Let's run this and you can see we have hello.
Okay.
So that's another example of a function that doesn't return anything.
And it also doesn't get any input parameters.
So to recap a function is a reusable block of code.
It has a name, it can have some input parameters, and it can also return something.
And there's a lot of flexibility.
You can call a function within another function.
You can combine functions together, etc..
Okay.
And a function.
As you can see, a function that you define does nothing on its own okay.
It's kind of a template that you can use to be able to use the function.
You actually need to call the function as we did here.
And finally you should always right.
So you should define the function before you use it.
Okay.
If I try to run this line before I define the function, it's not going to work.
So you always have to define the function first and then use it.
If you don't do that, you will simply get an undefined error.
Okay.
That says that for example the triple number is undefined and then you know that you need to define
it before you use it.
All right.
And we will come back very often to functions in this course.
We will use them everywhere.
So they will be more opportunities for you to practice.