How To Create A Function In Python

Create A Function In Python

Now, we are going to have a look at how we can create our own functions. It is very easy, and once you get familiar with the syntax, you would then be able to create many functions for doing different things. Before we address the thing, that how we can create our own functions, we need to have a look at some terms, like a function definition, and function call.

First of all, we are going to have a look at the function definition. The fact is that we are going to create our own function. There are many built-in functions, which means that they are already written, and we are going to just use them. But for the functions that we are going to write, we can say that as of now, python has no idea about them. So, we need to define the function. This is where the function definition comes in.

Also, when it comes to function calling, we have been doing it for a long now. Well, we know that print is a function. But it executes only if we are calling the print function. The same is with the other functions. So, we would be creating our own functions here. So, in order to execute our function, we need to call it.

We are going to have a look at both of these things, (function definition, and function call).

So, first of all, Let’s have a look at some examples, through which, we can understand how we can create functions.

Create A Function In Python

What you are seeing in the above program is just a function written. This means that we have not called the function, so it is not going to execute. But the thing is that we are right now understanding how to create some functions. So, Let’s focus on that thing for now.

So, the first thing that comes to our attention here, is the def keyword. The def keyword is important here. So, first of all, we give the def keyword, and then we are giving the function name. Now, for giving the function names, there are several rules, but we should name the function in such a way, that the name of the function defines what the function is doing. For example, in the above program, the name of the function is greeting, and it is greeting (for example).

So, followed by the name, we have to give the brackets. Giving brackets is also an important thing here. After this, we just have to give the colon and hit enter.

If you are using some IDE, like me, you would get some indent here, after you press enter. At this indentation level, we are going to write the statements that are going to come into the function. In the above example, we have only one statement, but let’s add some more here.

So, we got some more statements here, some of which are a part of the function, and some are not. Basically, you can just read that the last statement is not part of the function, since it is on a different indentation level. In python, the indentation is very very important.

So, simply we can say that there are some important things to understand, like the def keyword, the name of the function, the brackets, the colon, the indentations, and the statements (which are going to be according to what the function does). With time, as you keep on using the functions, you would get used to the things.

Now, we have written the function, but not called it, so the function is not going to execute. So, now Let’s have a look at how we can call the function.

So, as you can see from the above program, the last statement, is how we call the function.

So, if we execute the above program, the print statement would execute, and also after this, the function is going to execute.

So, Let’s have a look at the output now –

I am not a part of the function!
Hello from GyaniPandit!
hello again

As you can see, in the output, the print statement is executed, and then the function was called, so we can just see the function statements executed in the output.

So, this is how we create the function, and we have also seen that it is important to call the function every time we need to execute it. Once we start using the functions in different situations, we would get used to them. Now, Let’s get into further exploration in relation to the function.