Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Vs Function

 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

can some one tell me what is exact difference between method and function? Like c function.

Aren't they same?


- Vivian
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

They are same. in C programmer used to say function.

in the same way java programmer used to say method..
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each language has its own lexicon of terms with special meaning. In "C", the word "function" means a program routine.

In Java, the term "function" does not have any special meaning. Whereas "method" means one of the routines that forms the implementation of a class.

If you use the term "function" in informal discussions about Java, people will assume you meant "method" and carry on. Don't use it in proper documents or presentations about Java, or you will look silly.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "method" is a "member function" - a function that is embedded in a class. In some languages, such as C++, you can also have standalone functions -functions that don't belong to any class.

At least that's the terminology that I'm used to.
 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I generally think of a function as being in a non OO program- it is just a block of code that needs to be executed multiple times, and it is most convenient to put it into one nice little name that you can call from anywhere in the file. I generally think of a method as being inside a class, and is not a convenience but a necessity: everything is in a method. This is why I consider Java and C++ to use methods, and languages such as AS and BASIC to use functions. Also, functions are generally declared with the word 'function.'
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Users of functional programming languages (not to be confused with programming languages who's subroutines are colloquially called "functions") tend to think of a function f as a mapping from X to Y (f(X) = Y). Importantly a function will always return the same result when called with the same arguments. By that definition a java method like java.lang.StringBuilder.append(char) is not a function but java.util.Arrays.binarySearch(int[],int) is.
[ June 04, 2008: Message edited by: Garrett Rowe ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:
Importantly a function will always return the same result when called with the same arguments.


And it doesn't have any side effects.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


And it doesn't have any side effects.


That is most certainly not the case. Though if you would like to make a distinction between functions that cause side effects and those that map input to output, it can be useful.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Shirley:

That is most certainly not the case.


It most certainly is, in the terminology of the functional programming paradigm, which is what we were talking about: http://en.wikipedia.org/wiki/Purely_functional
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That indicates the main thing to learn from this thread. That is, particular words have different meanings, or no particular meaning at all, depending on what type of programming you're talking about. So it is important to make clear the context of the discussion.

In Java "function" doesn't mean anything special. In C, "function" means a program routine. In C++, it can mean the same as it does in C, or it can mean rather the same as "method" does in Java. In functional programming, a "function" has a very strict definition, much tighter than in the procedural/OO languages previously mentioned.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter, very nice summary!
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The languages OO use the term, method and languagens not O.O use function for example: php (today she implements O.O), C++, pascal etc...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic