Function and Method in programming language.

Generally in interview it is asked what is the difference between a method and a function.

We make use of two terms namely function and method in programming language. But there is difference between the two.

Function :-

  1. Function is defined in structured programming language like C, Pascal and in object based language like java script.
  2. Function is self describing code.
  3. Function is free from class that means we can define a function outside a class.
  4. Function is having its own existence.
  5. We can also say a function is not associated with an object.
Example:- 

void sum()
{
    int a=7,b=8;
    printf(“sum =%d”,a+b);
}

Method:

  1. The term method is used in case of object oriented programming language for example Java , C#.
  2. Method is defined inside a class.
  3. Methods are associated with an object that means a method can be invoked with the help of an object
  4. Methods are used to manipulate objects.
Example:-

class arithmetic
{
    int a,b,c;
    public void add()
    {
        c=a+b;
        Console.WriteLine("Sum of two numbers is : {0}",c);
    }
}