aspose file tools*
The moose likes Beginning Java and the fly likes Method Vs Function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Vs Function" Watch "Method Vs Function" New topic
Author

Method Vs Function

Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63
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.'


I never know what to put in my signature...
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Nov 08, 2007
Posts: 457
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.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Oct 30, 2001
Posts: 1970
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

Joined: Jul 11, 2001
Posts: 14112
Peter, very nice summary!
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
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...


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Vs Function