14 Replies - 430 Views - Last Post: 19 June 2013 - 01:54 PM Rate Topic: -----

#1 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Functions in Java

Posted 18 June 2013 - 10:20 PM

Java is my bread and butter, and I'm still only wetting my feet. I've studied through the SCJP study guide and a couple other Java related books. While thinking about programming related things, my brain decided to remember a word from the past. The past where I was sitting in school, not likely caring about what my Computing teacher was saying.

That word was function. The word function is not used in the books I've studied, that's why it caught my attention. I googled up on what a function is actually defined as in other languages.

"A method that does not require an instance is a function"

Is a static method a function, then? ... And if so, why is it called a static method and not a function?

If not, then how would you describe the difference between a method and a function to a Java programmer?

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Functions in Java

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,190
  • Joined: 11-December 07

Re: Functions in Java

Posted 19 June 2013 - 02:40 AM

I don't know the answer but I think it is a lot more woolly than that. In VB.NET, you have at least two kinds of method (for want of a better word) a Sub just does something without returning a value (like a Java void method) and a Function returns a value. You also have Shared Sub and Shared Function which are a bit like the static equivalents in Java.

There are also languages where classes are objects too (Ruby, Smalltalk and probably others) so a class level method would still have an associated Object instance.

Apologies if that's not a satisfying answer. It's just food for thought.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Functions in Java

Posted 19 June 2013 - 02:54 AM

cfoley is right - it is a little more woolly - not just a matter of static == function, instance == method.

In Java, i deliberately only use the word method, if only to shove the many people who come here using Java as a procedural language in the right direction
Was This Post Helpful? 1
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Functions in Java

Posted 19 June 2013 - 03:12 AM

Quote

I've always believed that a method is a function that lives inside a class. So therefore in Java you can only have methods as it is strictly OO.

No idea if this is even remotely correct but it makes sense to me.
That's fine by me. Of course, a function that lives inside a class in C++ is called ... a function ;)

This post has been edited by g00se: 19 June 2013 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,190
  • Joined: 11-December 07

Re: Functions in Java

Posted 19 June 2013 - 03:19 AM

The other side of the coin is what are callable blocks of code called when a functional language has OO features?
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Functions in Java

Posted 19 June 2013 - 03:40 AM

Quote

when a functional language has OO features?
What would be an example of that?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Functions in Java

Posted 19 June 2013 - 03:45 AM

OO Cobol
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,190
  • Joined: 11-December 07

Re: Functions in Java

Posted 19 June 2013 - 03:57 AM

Lisp is an obvious one. I'm sure I had seen some OO features in Haskell before but I can't find them now.

pbl, I've never used COBOL but I didn't think it was a functional language.
Was This Post Helpful? 1
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Functions in Java

Posted 19 June 2013 - 04:10 AM

Better ask pbl then ;)/> afaik Cobol most certainly is procedural. A general term for callable code is of course 'module'

This post has been edited by g00se: 19 June 2013 - 04:10 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#10 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Functions in Java

Posted 19 June 2013 - 09:48 AM

So the term "function" doesn't really have a place in Java, or is "method" just used lazily as a generic term while another name like "function" or "subroutine" could more accurately describe it's behaviour?

I feel like I'm trying to distinguish between a school of newly bought goldfish. They all seem pretty much the same to me right now.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7803
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: Functions in Java

Posted 19 June 2013 - 11:23 AM

Generically, the term function is used by many programmers to mean "something like a C function" - that is, a procedure that might take some arguments, and might return some value, and might have side effects, and might be tied to an instance of a class or not, or might not even know about classes at all. This is not a claim of correctness, just usage - this is what people say and what they mean when they say it. "Subroutine" and "method" are roughly synonymous.

As far as I know, there is no generally-accepted consensus on finer shadings within this, though of course the FP crowd wants desperately to have "function" reserved for the sense of "a block of code that you can pass around like in Lisp". That's not going to happen, frankly, and trying to use it in that sense is simply confusing for no good purpose.

That being said, in Java the things that we generically call functions happen to be called methods, and out of simple politeness, we should obey the "when in Rome" rule, and refer to them as methods. So if I'm talking about a piece of Java code, I'll talk about methods, and if I'm talking about VB i'll talk about subroutines. If I'm talking about a programming problem, I'll talk about functions - even if my example might be a Java method and a VB subroutine and a Lisp function. At least, that's the usage that seems to be common where I am, and it makes sense to me.

This post has been edited by jon.kiparsky: 19 June 2013 - 11:25 AM

Was This Post Helpful? 1
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Functions in Java

Posted 19 June 2013 - 11:53 AM

View Postjon.kiparsky, on 19 June 2013 - 08:23 PM, said:

though of course the FP crowd wants desperately to have "function" reserved for the sense of "a block of code that you can pass around like in Lisp".


Do you have a source for that?

I've seen many definitions of the term function in programming, but never one that required them to be able to be passed around as values (and I'm an FP person). Functions that can be passed around as values are often called first-class functions, but that certainly wouldn't imply that "callable blocks of code" that can't be passed around aren't functions - only that they aren't first-class.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7803
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: Functions in Java

Posted 19 June 2013 - 12:06 PM

No, of course I don't have a source for that. This is observed usage, not dictionaries. If this isn't your usage, I agree with you, but you'll notice it's implied even in the term "functional programming".
Was This Post Helpful? 0
  • +
  • -

#14 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Functions in Java

Posted 19 June 2013 - 01:00 PM

Quote

No, of course I don't have a source for that. This is observed usage, not dictionaries.


If you have observed that usage somewhere on the Internet, you might have had a link to that. I don't think that was a stupid question.

Quote

it's implied even in the term "functional programming".


To be honest, I don't have much knowledge of the etymology of the term "functional programming", but I know that it used to be somewhat common for programming languages to distinguish between functions that produce values and subroutines that don't like cfoley mentioned.

So it's possible that "functional programming" is simply called that to indicate that in functional programming you only (or mostly) use functions - not subroutines - as functional programming eschews side effects and subroutines are only useful for their side effects.

But of course, that's all just speculation. All I can say for sure is that I've never heard anybody use the term "function" in the way that you suggested the FP crowd desperately wants to have it reserved.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7803
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: Functions in Java

Posted 19 June 2013 - 01:54 PM

View Postsepp2k, on 19 June 2013 - 03:00 PM, said:

If you have observed that usage somewhere on the Internet, you might have had a link to that. I don't think that was a stupid question.


It wasn't a stupid question, but at the same time, I don't generally expect that anyone would have a citation for ordinary usage (or usage they think is ordinary)


Quote

All I can say for sure is that I've never heard anybody use the term "function" in the way that you suggested the FP crowd desperately wants to have it reserved.


Fair enough. On reflection, it might be more me hearing than them saying. I suppose I have a dim view of the more fervent advocates of "functional programming", and I take the worst of what they say as typical. Bad habit, I should cut it out.
(Mind you, this dim view does not extend to the capabilities afforded to me by languages like lisp, ruby, or scala, if I had the wit to use them effectively - just the people who have got the religion and want me to go to church with them)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1