HI WELCOME TO SIRIS

Difference between function and method

In programming langauages we have two concepts functions and methods. functions are defined in structural language and methods are defined in object oriented langauge. The difference between both is given below :

Functions

  1. Functions have independent existence means they can be defined outside of the class. Ex:- main() function in C, C++ Language
  2. Functions are defined in structured languages like Pascal,C and object based language like javaScript
  3. Functions are called independently.
  4. Functions are self describing unit of code.
  1. //function main in C
  2. void main()
  3. {
  4. int a,b,c;
  5. a=5;
  6. b=6;
  7. c=a+b;
  8. printf("Sum is : %d",c);
  9. }

Methods

  1. Methods do not have independent existence they are always defined with in class. Ex:- main() method in C# Language that is defined with in a class
  2. Methods are defined in object oriented languages like C#, Java
  3. Methods are called using instance or object.
  4. Methods are used to manipuate instance variable of a class.
  1. //method sum in C#
  2. class demo
  3. {
  4. int a,b,c;
  5. public void sum()
  6. {
  7. a=5;
  8. b=6;
  9. c=a+b;
  10. Console.WriteLine("Sum is : {0}",c);
  11. }
  12. }