Posted By : Shailendra Chauhan, 17 Jun 2012
Updated On : 17 Jun 2012
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
Functions have independent existence means they can be defined outside of the class. Ex:- main() function in C, C++ Language
Functions are defined in structured languages like Pascal,C and object based language like javaScript
Functions are called independently.
Functions are self describing unit of code.
//function main in C
void main()
{
int a,b,c;
a=5;
b=6;
c=a+b;
printf("Sum is : %d",c);
} Methods
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
Methods are defined in object oriented languages like C#, Java
Methods are called using instance or object.
Methods are used to manipuate instance variable of a class.
//method sum in C#
class demo
{
int a,b,c;
public void sum()
{
a=5;
b=6;
c=a+b;
Console.WriteLine("Sum is : {0}",c);
}
}