What are the definitions of method, function and procedure?

I always thought the definitions were these, but it seems I'm wrong:

  • Functions: every procedure that returns something

  • Methods: every procedure that returns nothing

  • Procedure: would be the classification basis of the previous ones, something that executes instructions, whether or not to return

I would like to know if these definitions are the correct ones, and if not are, or if they 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 on the side of mathematics.

Author: Miguel Angelo, 2014-04-04

4 answers

  • 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 definition of Delphi/Pascal/Visual Basic/Visual Basic .NET);
  • Method : procedure or function belonging to a class (several programming languages define this way, for example, c++, c#, java, etc.).

There is an issue in Programmers where this is widely debated, but the consensus is that.

 19
Author: Leonel Sanches da Silva, 2017-04-12 07:31:59

The definition of method from the English Wikipedia says that it is a procedure associated with an object, and 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 sequence of instructions specific to a program, which can be invoked from other locations.

Already one function it refers to something that returns a value, analogously to mathematics. It would be a set of statements that returns a value at the end. A function is a procedure, but with that detail of the return to more.

Um Method it can be a procedure or function, but associated with an object or class. So it can be called a "member function".

Practical examples

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

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

Already object-oriented languages like Java or C# always use the term method to refer to procedures associated with classes.

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

Conclusion

While I have not cited strong references from authors, I think Wikipedia's content is consistent and the definitions make sense generally across all platforms I know of.

 19
Author: utluiz, 2014-04-04 20:33:23

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

In algorithm functions, also known as subroutines, always return some value. One of the great benefits is that you don't have to copy the code every time you need to perform that operation, as well as making reading the code more intuitive. For example: Vc can create a function to calculate the square root and return a value. Whenever you need, the same function will be called and you don't have to recreate the calculation multiple times in your code. Vc create only once.

Procedures differ from 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 saves the text, and the second only prints on the screen, not returning values for be used.

And the method, is it a function or a procedure?

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

 2
Author: Carlos, 2016-10-07 23:23:39

Method

Is the responsibility a class. In software engineering, when you do the responsibilities of a class in a class diagram, those are the methods.

Procedure

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

Function

Is a piece of code (subroutine) with return value.

 0
Author: guijob, 2016-10-07 23:22:48