What is a function in python | what is a method in python?

On the road to become a successful developer we will come across many concepts some of are easy some of are difficult to grasp.

But some of are extremely easy and we might have not given a slightest effort to know what it is.

One of is what is function in python or what is method in python. Let’s simply begin with exactly function actually is.

A function is a block of code which is written once and only once and can be called every time to perform repetitive tasks.

Meaning you want to add two numbers and you wrote code for it, so what you will do is write those code in a manner in which it can be used again whenever we want to add two numbers. Basically, functions are written to increase efficiency.

Now that we know what function is we can understand what actually method is?

A method is nothing but a function written inside class is simply called as method, and if it is written outside class, it is called function.

Types of function in python.

There are basically 5 types of function but mainly we focus on or deal with only two types.

  1. Positional argument function
  2. Keyword argument function
  3. Default argument function
  4. Variable-length argument function
  5. Variable-length keyword argument function


  1. Positional argument function.

Positional argument function

functins in python

When we pass the value directly to function while calling it. These types of function are called positional arguments.

The reason why they are called positional argument is because they accept its value in certain positions only.

If I had passed ‘Khan’ first and ‘Waqar’ in second position the output would have been different. That is the reason why it is called positional argument.

Positional argument has the highest priority, meaning if we specify any type of argument before it, that will throw an error.

  1. Keyword argument function.



While calling function when we specify the value of function in parameter with assignment operator these types of function is called keyword argument function. 

As you can see, we have changed the position of arguments, but with the help of parameters (Fname, Lname), it will not affect the overall code because we are explicitly passing values to arguments.

This is called keyword argument function.

Keyword argument follows positional argument in priority. Meaning if we specify keyword argument first and then positional argument we will get error.

  1. Default argument function.



Default argument functions has some values already inside it at the time of function declaration and this is used when user does not pass some values at the time of function calling.

Meaning when user calls default argument function and if he passes some values the value will be passed to function body, if he does not pass any values then function will accept default value which was declared at the time of function creation.

You can see that I only passed single value at the time of function calling, the other two values are taken as default by function itself.

Now if I had to change the branch name to mechanical engineering, I have to use keyword argument at time of function calling and here is the reason why keyword argument.



As you can see, I have passed ‘ME’ as second value but instead of going into branch it is going into university parameter, and here is the reason why, because while calling function we are following positional argument function patterns so at whatever positions we are passing values it’s going there.

To overcome this issue, we will simply use keyword argument function. 



  1. Variable-length argument function.



This is nothing but when we want to pass n numbers of positional argument into function body then we use this type of function. The arguments type is always tuple.

Now suppose if want to pass 10 arguments into function, then we will not define 10 parameters to hold those arguments, we will simply define a single variable and a * before that variable, now that variable can hold any number of arguments.

  1. Variable-length keyword argument function.



As the name suggests, variable means changing, keyword argument means it has something to do with keyword argument function.

So basically, when we do not know how many numbers of keywords arguments, we are going to pass we use this function.

We can pass two keyword argument function or even we can pass ten keyword argument function.

Difference between *args and **kwargs.

There is a lot of confusion among every one of us about what is * doing in function what is args what is kwargs.

Basically, this is the difference between variable-length argument and variable-length keyword argument.


This is variable-length argument.

This is used when we want to pass n numbers of positional arguments to function or we do not know how many arguments will user pass into this function then we use *args.

This ‘*’ represents that this function is going to accept n numbers of positional arguments only. All the arguments going into this is a tuple.


This is variable-length keyword argument

This is used when we want to pass n numbers of keyword arguments to a function, meaning when we do not know how many numbers of arguments the will user pass, then we use this function.

This “**” represents that this function is going to accept n numbers of positional arguments only. All arguments going into this are dictionary type.

We can have any name instead of args or kwargs.


Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published.