Y
Y
Yoricrayn2018-04-03 23:12:33
Programming
Yoricrayn, 2018-04-03 23:12:33

What is the difference between the concepts of function, procedure and method in programming?

Periodically I see all 3 definitions. Is it the same or something different, and which of the concepts is the most correct to use?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
D3lphi, 2018-04-03
@Yoricrayn

A function is a subroutine that performs some operation and returns a value.
A procedure is a subroutine that only performs operations, without returning a value.
A method is a function or procedure that belongs to a class or class instance.

#
#, 2018-04-04
@mindtester

D3lphi

A function is a subroutine that performs some operation and returns a value.
A procedure is a subroutine that only performs operations, without returning a value.
A method is a function or procedure that belongs to a class or class instance.

sort of yes, but... only at the very initial stage, in order to calm the chaos in the beginner's head))
in the future, all these concepts are context-dependent, the context is the programming paradigm and / or a specific language
1 - in the context of paradigms, from One of these concepts is unique Method , as already mentioned by D3lphi , is something that belongs to the class. class, in turn, is a fundamental concept of OOP based on classes (the charm of the situation is that OOP is also different;))
in this case, it is more common to clarify that a method is one of the types of class members (there are also fields , properties , interfacesbut this is already the context of a particular language) .. and as Griboks rightly noted - it is implemented by a function or procedure,
but .. there are many languages ​​where there is no concept of a method at all,
but there is also functional programming .. this paradigm is partially present in many modern languages, however, there are languages ​​where any code is only a function
2 - from the context of languages: the
concept of a procedure in an explicit form, most often used by teachers who themselves studied in languages ​​such as Fortran , Pascal or related, and either had no other experience at all, or other experience was much more modest
now languages ​​based on C syntax dominate , even java and js in this matter will become relatives of classical C
and there is no concept of procedure in it , only functions .. and in the case when the function is not required to return any value, the return type is simply indicated void
mixing it all up with C# as an example - in this language, everything is an object . and any executable code is a method , and methods are implemented only by functions (including void functions)

E
Eugene, 2018-04-03
@klim76

I'll take part too :)
in functional programming
in procedural
in the PLO

G
Griboks, 2018-04-03
@Griboks

A procedure is a subroutine in a separate scope.
A function is a procedure that returns a value.
The method is either the first or the second.

A
awesomer, 2018-04-04
@awesomer

Essentially, nothing.
In fact, little things.
For a method, the parameter is automatically the object of the class to which the method refers.
But no one bothers you to pass an object of the class explicitly as a parameter.
True, the class method will automatically have access to the internal protected fields of the class, which the function to which this class object is passed as a parameter will not have.
But it depends on the language. For Go, for example, if a function is defined in the same package, then there are no restrictions on access to internal non-public fields of structures.
Function vs Procedure - the result of the function can be directly assigned to a variable
on the other side, and with the procedure you can do the same, but a bit more complicated:

function(&b)
a = b

That is purely for convenience.
Although I would not want to give up all these amenities.
In many other languages, the same keyword is used, for example, func,
but in fact it can be both a procedure and a method, and actually a function.
Which should not be misleading.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question