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
// C program to demonstrate a Function Â
#include <stdio.h> Â
// Declaration of function int func() { Â Â printf ( "\n FUNCTION" ); // statement } Â
int main() {     func(); // calling of function     return 0; } |
Implementation of Function in Object Oriented Programming Language(JAVA):
Java
/* a method is similar in working to function but it belongs  or is associated with the instance of a class i.e. object  */ Â
import java.io.*; Â
// Declaration of class 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(); // here you can see method belongs to                      // instance of a class named demo and is                      // called using object(test) of a                      // class(demo), so it is a method     } } |