-3

I understand the difference between methods and functions. I also understand all methods are functions but not all functions are methods. I always refer to methods as functions. It feels like a more natural term, probably because in PHP the word function is right next to the name of the method. However method seems to be a more precise word. I'm wondering if I'm misusing the term function and if it's a bad habit.

Is this use of the term function when referring to methods frowned upon or problematic?

Goose
  • 1,858
  • 2
  • 15
  • 27
  • @Polygnome I understand the difference. I'm asking if it's inappropriate to use the term function when referring to a method. Have edited to explain. – Goose Jun 14 '17 at 14:26
  • 1
    Read the answer(s) on that question. In most cases, there is none, except when certain languages give them a certain meaning. In Java, we have lambda *functions* (because they are not associated with an object context) and *methods* who are part of a class, whilst in C++ what Java calls methods is called "member functions". – Polygnome Jun 14 '17 at 14:28
  • @Polygnome Thanks, but that doesn't answer the question for me of if it's inappropriate to use the term function to refer to a method. – Goose Jun 14 '17 at 14:29
  • 1
    In order to understand when its appropriate, you first need to understand what each *is*! What is - to you - a function? What is a method? If you can answer those two question then you will already have answered wether or not its apprioate to call each the other way. And thats exactly what the linked question does. And, as I said before, it heavily depends on the concrete language you are using / the environment you are in. Every java programmer you talk to will associate "method" with "executed in object context", whilst a C++ programmer will not (they are called member functions in C++). – Polygnome Jun 14 '17 at 14:32
  • 1
    For php, *I* still call only functions that are class members *methods*, while I would expect a function *not* to be a class member. But I'm pretty sure that there is no globally accepted consensus on that (at least not in PHP, where objects were introduced with PHP4 I beleieve and were not part of the original language, but my memory is fuzzy on the beginnings of PHP). – Polygnome Jun 14 '17 at 14:36
  • @Polygnome Those comments are actually an excellent answer to my question. – Goose Jun 14 '17 at 14:42
  • 2
    Its only what is written in the answers on that other question, though the accepted answer on that question uses Pascal and C, not Java and C++.... The last sentence in that answer even is "The point is, none of this is really consistent. It simply reflects the terminology employed by whatever languages are en vogue at the time.", which is exactly what my comments here said. So yeah, I still think this is a duplicate. – Polygnome Jun 14 '17 at 14:59

1 Answers1

-3

Homonyms usually become more dangerous to use the closer they are. No one is going to confuse your use of "table" (array of numbers) with a "table" (wooden furniture), but "function" (piece of code) vs. "function" (mapping from input to output) is often not disambiguated by context.

Therefore, the term "function" to mean "procedure" is problematic: it's close to the meaning of "function" in a closely related field (maths), but categorically different in an important respect (a mathematical function yields a value, while a "function" that is really only a method may not). This makes the term awkward and liable to cause serious misunderstanding.

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
  • 2
    Thats for functions in mathematical sense. In programming, the difference between *functions* and *methods* is usually that the latter are executed in an OO context, while the former are not (although in C++, those are usually called "member functions"). While your answer might give a glimpse at how it *should* be, it doesn't reflect actual usage. – Polygnome Jun 14 '17 at 14:26