Method and function difference: The Subroutines | Programming

Method and function difference: The Subroutines

The difference between terminologies used for subroutines i.e. method and the function in any programming language.

function and method difference

A function is a block statement(s) usually named for repeaded usage. A method is same as function but the method is declared in a class and called using object reference or with class reference (in case of static method)

functions and methods, both are subroutines, declared to use multiple times in a program/software application.

functions

annonymous function

(function(){ alert(new Date()); })();

named function

function dateTime(){ alert(new Date()); }; dateTime();

methods

Methods in a class are always given name because they are to be invoked (call) by name with their object (reference)

class Test{ function dateTime(){ alert(new Date()); }; } var test1 = Test(); test1.dateTime();

A programming language allocates memory for each function/method only once. No matter how many times the function/method is invoked (called). The programming language compiler or interpreter uses the same function from the memory by passing arguments in the parameters, again and again.

  1. Functions can be declared anywhere in a program e.g JavaScript, c etc.
  2. Methods are declared withing a class
  3. Either its function or a method, placed in the memory