Are functions only in non-object oriented languages?

I was asked to answer this question:

Where should I put Jakadoc specific comment notation / ** and * / if I want to tell the user about some instance variable or method?

I replied:

Above function declarations.

The answer was rejected, and this was the reason:

Functions are in non-object oriented languages. Method is the correct name.

It's true?

+4
source share
3 answers

Are functions found only in non-object oriented languages?

No. There are object oriented languages ​​that have functions. C #, for example, is an object-oriented language, but has anonymous functions.

What are named procedures that are members of a type commonly called in object oriented languages ​​like Java or C #?

Usually they are correctly called methods, although this differs from language to language. In Java or C #, I would say "method".

In Visual Basic, for example, a distinction is made between functions and routines based on whether they return a value and not based on whether they are associated with a type container.

JavaScript, an object-oriented language that uses prototype inheritance rather than class inheritance, usually refers to all of the above as "functions."

Do people often refer to methods as functions, speaking casually about Java or C #?

Yes. If I were writing documentation or a book or a scientific article, then I would be careful to make a distinction. In ordinary language, although everyone reasonably familiar with the art of computer programming would understand “function” and “method” as synonyms. I would not refuse your answer.

+17
source

Any answer that limits this to a particular language is inherently erroneous. In addition, you must also deal effectively with static methods and routines.

Computer science began with the term subroutine . Small sections of repeatable code that can be executed arbitrarily to perform a common action. Examples can be found in early programming languages ​​such as BASIC.

Functions are the evolution of routines. They accept arguments and may or may not return a value. They take some concepts from mathematics - input, are translated to a given conclusion.

With objects, we should be able to call actions on objects, and we do this by providing methods . Like functions, they take arguments and may or may not return a value.

Static methods are designed to work with all possible objects of the class.

The problem is that pure object-oriented programming leaves no room for defining functions (or indeed routines). And languages ​​that evolve to become object oriented often retain the syntax of functions for implementing methods.

In Java, we use the 'Utility' classes to provide functions as public static . The Math class in JavaScript is another example of this.

In PHP, we allow the use of the word function to define methods.

In C ++, we see both functions and methods that are not differentiated. In fact, C ++ does not refer to methods, calling them member functions.

+1
source

The function is not related to the class.

A function is something like doStuff(); .

The method is similar to someThing.doStuff(); or SomeClass.doStuff(); .

In Java there is no such function as a function. These are all methods. i.e.

  class Test { public static void doSomething() {...} public void otherThing() {...} public static void main(String[] args) { doSomething(); //implied Test.doSomething(); } public Test() { otherThing(); //implied this.otherThing(); } } 
0
source

All Articles