Difference between function and method

05 Sep 2022
Intermediate
10.4K Views

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 a 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 within a class. Ex:- main() method in C# Language that is defined within 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 a manipulate the 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);
 }
} 
Read More Articles Related to oops
Summary

In this article, I try to explain the difference between method and function. I hope after reading this article you will be able to understand the difference between method and function. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Learn to Crack Your Technical Interview

Accept cookies & close this