When we are working in Opps then we often use a term method and a student who came from C language uses a function. So, there are always a confusion between methods and function. so in this topic we’ll see how methods are different from function.
Basically, In C language we use a function because C is a procedural programming language and in procedural programming, the task is divided into functions. On the other hand, In any opps language such as C++, java Python etc. the task is divided into objects that are form through classes to accomplished their task.
we can say that method is like a function but not same. There are few points that make a method differ from function. The points are as follows.
- Methods can only be created with classes.
- Each method require an object to call.
- since the methods are associated with classes therefore, it can’t access any other data members It can only be access the instance variable (data member).
- It provide data security.
- In python remove (), appends () etc are the methods that is the class type list and In order to use these methods we need a list object as given below.
list = [4,2,3,5] list.append(10) # it will append the value 10. list.remove(2) # it will remove value 2.
A function is a block of instruction/statements that can be repeatedly use by just calling it. A function doesn’t need any classes to create it hence it is not dependent on any object. Function are less secure than methods that’s why opps are highly recommended to use. The function max () min () and len() are the few example of function in python programming.
list = [10,30,4,223,51] # min is a function that is taking one argument and it is independent to object. print(min(list)) # max function return the maximum value of the passing argument. print(max(list))
These are few points that makes the difference between a methods and functions.