Difference Between Function and Method
Function: A Function is a reusable piece of code. It can have input data on which it can operate (i.e. arguments) and it can also return data by having a return type. It is the concept of procedural and functional programming languages.
Method: The working of the method is similar to a function i.e. it also can have input parameters/arguments and can also return data by having a return type but has two important differences when compared to a function.
- A method is associated or related to the instance of the object it is called using.
- A method is limited to operating on data inside the class in which the method is contained.
- It is a concept of object-oriented programming language.
In simple words if a function is part of an instance of a class i.e. (Object) then it is called method else it is called function.
Function
|
Method
|
It is called by its own name/independently. |
It is called by its object’s name/referenced. |
As it is called independently it means the data is passed explicitly or externally. |
As it is called dependently which means the data is passed implicitly or internally. |
Implementation of Function in Procedural Programming Language(C):
C
#include <stdio.h>
int func()
{
printf ( "\n FUNCTION" );
}
int main()
{
func();
return 0;
}
|
Implementation of Function in Object Oriented Programming Language(JAVA):
Java
import java.io.*;
class demo {
public int method()
{
System.out.println( "METHOD" );
return 0 ;
}
}
class GFG {
public static void main(String[] args)
{
demo test = new demo();
test.method();
}
}
|
Last Updated :
19 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...