Welcome back to the course, in this section we are going to talk about how you can define your own function. You can use D F, F, to define your own function here. So we type in D F, and then say underscore Hello, and bracket. So say hello will be our function name. And then in our next night, will you type in print hello world. This will be our function content.
And then we we call the function we type in say underscore Hello bracket. And then you can see the result will be hello world because Python is now executing the command in the function. And there are some shortcuts in Python which are Ctrl N and Ctrl p Ctrl p for previous command and control n for next command. All right, now we are going to talk about our next assignment, which is the BMI function. The BMI function will require the height and the weight input by the user. So we use def to define this BMI function.
And then we type in the function content height equals to input blanket. How tall are you? Question not. And then we type in which equals to input bracket. How much do you wait? As you may already know, the BMI is calculated by the weight divided by the square of height so we can type in p MPI underscore Wario equals to weight, smash height and then the posta two, which means the power of two.
And because weight is a string, we need to convert that to an integer by using the I nt function. Same for the height value as well. And height should be measured in meter. So here we need to convert height from centimeter to meter by dividing it by 100. Finally, we need to make sure we calculate the height conversion first. So, we put it inside a bracket.
The final formula will therefore become the weight divided by the height committed in centimeter and square. And then in our next night, we will type in print BMI underscore fair Okay, so we have defined our own BMI function. When we call the function you can see, we are asked to input the height, and then the weight and then hyphen will give us our BMI value. Now this value has a lot of decimal places. If you want to run that, we can use the one function in Python. We can go back to our previous command by using Ctrl P. Once we get back to our source code, we go to the last night and insert a pin function with one function.
The one function takes two parameters. The first is the BMI value. And then the second one will be the number of decimal places that we want. Here we put into now after we have changed the function, we call the BM function again Input the same values. You can see there that we saw has been wanted to two decimal places. So this is how we define our own function.
In the next section, we are going to talk about the if else condition.