Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I need to know the difference between method and function in detail. As i know, there is no such difference but methods are predefined and functions are user defined set of codes/lines.

Thanks in advance ..
Posted

Roughly speaking: a function operates just on the passed parameters while a method, being exposed by an object, operates on the object's internal state too.
 
Share this answer
 
Comments
nv3 23-Jul-12 9:49am    
Simple and short! And correct!
Richard MacCutchan 23-Jul-12 11:34am    
In C++ methods are referred to as Member Functions. It's really just a question of "what should we call these things?".
Malli_S 24-Jul-12 1:58am    
Short, simple and exact! My 5.
The answer is Semantics...

The followup question is, what answer is your instructor expecting?

But as was answered above..., usually..., a method is a function associated with an object.

In C++ it's defined as a member function of a class.
 
Share this answer
 
About the only thing that you can say for certain is that a method is associated with a class, whereas a function need not be. Otherwise, the two words are used to mean similar things in different contexts.

In the world of mathematics and functional programming, a function takes its input values from its parameters only, and affects the environment only by its return value. Mathematically, a function transforms values to other values, as opposed to an operator which transforms functions to other functions. For example, + and - are functions (not operators), as is { x => x + 2 }. This terminology is used in functional languages and in the APL family of languages, but loosely, so that you still call something a 'function' even if it reads or writes global state.

In the world of conventional procedural programming, i.e. C and non-OO C++, any subroutine is called a 'function'. In C++ this is extended to the OO parts of the languages, as in 'member functions', 'pure virtual functions' etc – and in those cases the word has a complete overlap with 'method'.

In conventional object oriented programming, i.e. Java and C#, any subroutine is called a 'method'. In code teams which have experience of a functional approach, methods which operate only on their parameters and write only through their result are sometimes called 'functions' or 'pure functions'; these methods will generally, though not always, be static, because they don't need to read or write instance state.

In Pascal and Delphi, a subroutine is called a 'procedure' if it returns no value, and a 'function' if it returns one, regardless of considerations about state. This definition is included in the language itself; you define the routine with the word 'procedure' or 'function'.

In dynamically typed languages with closures and the ability to pass references to subroutines around (e.g. JavaScript), routines which can reasonably be passed around in this way are often called 'functions', and that gets extended to all other subroutines.
 
Share this answer
 
Comments
Richard MacCutchan 24-Jul-12 7:48am    
Good detailed description. Thanks also for the bit about Pascal, I knew I had used something like that years ago but couldn't recall which language it was.
Quote:
A function is a piece of code that is called by name. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).

All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by name that is associated with an object. In most respects it is identical to a function except for two key differences.

It is implicitly passed the object for which it was called
It is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data)
 
Share this answer
 
A function fits in the style of procedural programming. A piece of code programmed that way is a collection of data, that can be accessed by any function, and functions that can operato on that data. (I am simplifying a bit here)
A function has a name, and a signature. The signature is defined by the return value and the parameters that are being passed.
To call a function in a program, one simply uses the function name.
C#
void foo(){
  printf("foo\n");
}

void main(){
  foo();
}

A method fits in the style of object oriented programming.The idea is to define classes, where classes are a collection of data together with the functions (that are called methods now) that operate on that data. This is called encapsulation. The reason for doing this is, is to obtain a better maintainable program. By doing encapsulation the data dat belongs to a certain object can only be modified by the methods that have been provided by the class. An object is an instance of a class.
C#
class myclass{
public:
  int geta();
  int getb();
  int getMultiply();
  myclass(int a,int b);
private:
  int _a; 
  int _b;
}

myclass::myclass(int a,int b)
  :_a(a),_b(b){}

int myclass::geta(){return _a;};

int myclass::getb(){return _b;};

int myclass::getMultiply(){return _a*_b;};

void main(){
  myclass instance(5,6);
  printf("multiply=%d\n",instance.getMultiply());
}

to call on a method, one has to provide the object on which the method is to operate, and the method name.

I am only scratching the surface here, the best way to find out about a method is to read the first chapters of a book about OO.
 
Share this answer
 
v2
A method may or may not return a value, but a function always returns a value.
eg.
C#
//this is method
public void a Any_Method()
{
  //your code
}
//this is a function
public int Any_Function()
{
  int i;
  //your code
 return i;
}


If you want to learn more about method and function then just use google.
Good luck.
 
Share this answer
 
v3
Comments
StianSandberg 23-Jul-12 9:16am    
I agree with you! But there are a lot of discussion regarding this topic, and my conclusion is that this depends on programming language. And there are no correct answer for this..
But I do agree with you. That is my opinion also.. :) 5'd
Richard MacCutchan 23-Jul-12 11:32am    
Not true, there are lots of methods in .NET that return values.
Raje_ 24-Jul-12 0:36am    
Yes you are right. I have improved my solution.
Richard MacCutchan 24-Jul-12 3:37am    
Not true, there are lots of functions in C/C++ and other languages that do not return values.
Philip Stuyck 24-Jul-12 4:47am    
This answer and example is totally wrong.
a method will have in its definition always a reference of some kind to a class.
In c++ this means that the class name is mentioned before the method name.
ie someclass::somemethod
where in other languages the method will be embedded in the class
class someclass{
void somemethod();
}
There is no limitation on the signature of the method, it can return void or a value, it can have parameters but it does not have to either.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900