What are the definitions of method, function and procedure?

Asked

Viewed 22,728 times

24

I always thought the definitions were those, but it seems I’m wrong:

  • function: every procedure that returns something

  • methods: every procedure that returns nothing

  • procedure: would be the classification basis of the above, something that executes instructions, whether or not to return

I would like to know if these definitions are correct, and if they are not, or are partially, in which contexts these variations would occur.

Obviously, I’m only interested in definitions that exist in the world of programming... at most going a little bit to the side of mathematics.

  • I didn’t vote to close but that question seems based on opinions, some languages do as pascal make differentiation between function(returns something) and procedure(returns nothing), others do and some understand that the last instruction of a function/procedure is the return. Generally method is associated with object orientation.

  • 2

    Based on the opinions of whom? Of the creators of languages... for me it can be, those are the ones I want: the official definitions.

  • 1

    Although these definitions vary from language to language, this does not mean that they are opinions.

  • 1

    It is not based on opinions. Concepts exist and are properly postulated.

  • 1

    I’ve been doing some research, and I ended up finding one more term: subprocession. I remember that VB6 had SUB/END SUB, that must be it.

  • 1

    Sub is the equivalent of Procedure.

Show 1 more comment

4 answers

20


  • Procedure: Part of a program or class that does not return a value (from the Delphi/Pascal definition). In Visual Basic/VB.NET, it is also known as Subroutine (Subroutine, or simply Sub);
  • Function: Part of a program or class that returns a value (from the Delphi/Pascal/Visual Basic/Visual Basic . NET definition);
  • Method: Procedure or function belonging to a class (several programming languages define this way, for example, , , , etc..).

There is a question in the Programmers where this is widely debated, but the consensus is that.

  • 2

    I understood the answer but it got funny. Procedure and function are methods and method is a procedure or function.

  • It is. Let me see if I improve it.

  • But in VB the Sub no return. So the Sub I think it would fit with the definition of procedure. In VB remember that has Function and Sub.

  • I edited again. If I remember any more cases, I improve the answer, but I think that’s it.

19

To definition of method of the English Bible says it’s a procedure associated with an object, which can also be called member function. Static methods would be those associated with a class.

Often the terms function, method, procedure, routine and subroutine are used interchangeably to refer to the same thing, but there are some nuances.

I see no difference between procedure, routine and subroutine. They are synonymous with a specific sequence of instructions for a program, which can be invoked from other locations.

Already a function refers to something that returns a value, analogous to mathematics. It would be a set of instructions that returns a value at the end. A function is a procedure, but with this extra return detail.

A method can be a procedure or function, but associated with an object or class. Therefore it can be called "member function".

Practical examples

Procedures are used in languages such as SQL (T-SQL, PL/SQL, etc.) for routines that do not return value. Already functions are used for routines that return values. The same goes for Visual Basic.

Several languages that have first-class functions, such as Javascript, have the function declaration with the reserved word function.

Object-oriented languages such as Java or C# always use the term method to refer to class procedures.

In PHP, which is object-oriented and functional, the term function is used to refer to routines called directly in the code, while the term method is reserved for the object orientation part, which is nothing more than functions within classes. PHP makes no difference between function that returns value or not.

Completion

Although I have not cited strong references from authors, I think that the content of Wikipedia is coherent and the definitions make general sense on all platforms I know.

  • In javascript then, a method would be a function that is associated with one of its properties?

  • 1

    @Miguelangelo In Javascript, "method" is a function that belongs to an object (as property).

2

To conceptualize function, method and procedure, one must take into account the basis of programming that is the study of the algorithm:

In algorithmic functions (functions), also known as subroutines, always return some value. One of the great benefits is not having to copy the code every time you need to perform that operation, and it makes reading the code more intuitive. For example: Vc can create a function to compute the square root and return a value. Whenever you need it, the same function will be called and you will not need to recreate the calculation several times in your code. You create only once.

The procedures (procedures) differ from the functions only by not returning result. Example: to read the value typed by a user we use the READ procedure and to show a text on the screen we use the WRITE procedure. The first only stores the text and the second only prints on the screen, not returning values to be used.

And the method is a function or a procedure?

A method can be either a function or a procedure. We call a method function or procedure in object-oriented programming when these are associated with an object or class. Method is the name given to functions and procedures in object-oriented languages, only for a conceptual question of these languages. Deep down, they end up being the same thing.

0

Method

It’s a class responsibility. In software engineering, when one makes the responsibility of a class in a class diagram, those are the methods.

Procedure

It is a piece of code (subroutine) with no return value.

Function

It is a piece of code (subroutine) with return value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.