Pages

Friday 11 January 2013

Difference between function and method

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.
 //function main in C
void main()
{
 int a,b,c;
 a=5;
 b=6;
 c=a+b;
 printf("Sum is : %d",c);
} 

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.
 //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);
 }
} 

No comments:

Post a Comment