Friday, October 25, 2019

What is the difference between function and method?

First let's understand function. In C programming language, we use functions which contains a piece of code and every function must have a unique name with which it should be called or invoked.

Example:
#include<stdio.h>
int sum();
void main()
{
  int z;
  z = sum();
  printf("z = %d",z);
}
int sum()
{
 int a=5, b=9, c;
 return c=a+b;
}

In Object-Oriented Programming languages we use objects to invoke methods.
Syntax: Object.Method();

Note: In C langugae we don't have concept of class and object.

Function does not belong to any object or class.
Method belongs to a class.

All methods are functions, but not all functions are methods. 

No comments:

Post a Comment