After all, you learn about loops, conditions, casting, data type variable, and a lot of things.
Now it is time for learning something about functions.
Okay, first let's create a punk.py file and we are going to learn function examples and everything
here.
Okay.
What is a function.
A function is a block of code which only runs when it is called.
Okay.
You can pass data known as parameter into a function.
And also a function can return data as a result.
Okay, so how can we create a function.
Okay.
Actually we have two kind of function okay.
One of them is built in function.
And also pre the function that you can define it okay.
So in Python a function is defined using a def keyword or def like this.
And let's create a function.
Name it my function.
And like this you can create a function like this.
Okay you use diff keyword and then you give a name to the function.
And this use this parenthesis and use colon head enter.
And right here for example let's print a text here and say hello from a function.
Very easy.
Okay.
Now if I run this it will do nothing and I will tell you why.
Let's use Python function.py.
And you see that we have no output.
Why?
Because we haven't called this.
Okay, I told you, function can be run and can be executed whenever it is called only it is if it is
called so it will be executed.
If not, it is not going to be executed.
So to call a function, use the use the function name followed by parentheses.
So here let's use my function and then use these parentheses.
Now let's save it and then run it again.
You see now we have it.
Hello from a function.
So again if I use Ctrl c Ctrl V and save it again if I execute this code again you see that this time
it gives me twice.
And as much as I ran.
As much as I call the function, it will print me this text, this function.
So also we have parameters.
Information can be passed to function as parameters.
So parameters are specified after the function name.
Okay.
Here inside.
Okay.
Inside the parenthesis.
This parenthesis Parentheses.
You could, uh, you can add as many parentheses parameters as you want.
Okay.
You can add a lot of parameters here, but just be careful to separate them with a camera.
How can we do that?
Let's, uh, in this function, let's say we have, uh, something like if name.
Okay, anything.
And right here I pass a parameter by this name.
And I'm going to use that here if name okay.
And function name.
And here I am going to pass this if name here.
Because while I created this function I told it that it needs to get an argument.
And whenever I am calling the function I need to pass something to this.
Okay, so here I am going to.
Say my function Okay.
It is something that you pass.
You could write anything here.
Now, if I execute the program again, you say that hello from from my function.
From my function.
Okay.
So it is how you can pass a parameter to a function.
And also we can um, we can use a default parameter, which is very, uh, very good sometimes.
For example, right here.
Um, I'm going to pass a default parameter to this if name is equal to my function.
And okay, by default it is my function.
And right now it is not necessary to give this okay.
It's optional now.
Okay.
See hello from my function.
It comes from here.
But if I use Ctrl C and Ctrl V and this time I give something else, for example, name it and execute
this.
So this time it will give me hello from name instead of my function.
It will give me this one.
Now if I comment this you can comment in Visual Studio Code using Ctrl and hitting the slash.
So it will give you a comment.
So here see here.
Hello from name.
It is how you can use a default parameter using function and also function can return something.
Okay, the later function will return a value.
Use the return keyword or use the return statement.
So now let's.
Use another function and I'm going to name it this time.
Mull means multiplication.
In here I'm going to give an argument that is going to be x.
Here I'm going to return.
For example, two is going to be multiplied by x.
And that is it.
Now whenever I call this function and give a number to this.
So it needs to multiply that number by two and then return the result for me.
Let's print that function mul.
And here give the number two and save it.
Then call it.
It gives me four.
Why?
Because four comes here and then two comes here that we give it right here.
Okay.
Two multiply.
Two is equal to four.
Now if I give four.
Now you know that it is eight.
Okay.
That is how you can use return keyword with function.