What is the difference between functions and procedures? [duplicate]

7

I'm studying algorithms and I'm having a hard time understanding the difference between them and when to use these sub-algorithms in a program. I'm learning to program with portugol algorithm.

    
asked by anonymous 09.04.2016 / 03:54

1 answer

10

Confusion

Many seasoned programmers may have difficulty understanding the difference between one and the other. Both confuse that some people think that languages of the functional paradigm are the languages that have functions and the languages of the procedural paradigm are those that have procedures. What a complete nonsense, because deep down both are the same thing.

In the background it's the same thing

It is rare to find language that only has procedures (some SQL dialect may be, some very old and virtually abandoned language, or some very specific niche)

In a few programming languages there is a clear distinction between the two, others prefer to treat everything as functions. Some even treat functions only as methods.

In the background both the two questions in the question, how methods work in the same way and concretely the implementation is essentially the same.

What differs

Conceptually a procedure differs from function by the absence of a value return .

Then a procedure is an algorithm that will be executed, whereas the function is an algorithm that will execute and produce a concrete final result that can be used by another algorithm.

Pure function

As the term "function" comes from mathematics it should only perform a calculation and return the result without doing extra operations, without making input and output, without changing state out of its execution (only moves local variables, created within the function), in short, the function should be pure (deterministic, without side effects). Only a few functional languages really do require purity. The practice of purity is little pragmatic for most of the problems commonly encountered in computing. Inotherwords,thefunctioncomputessomething(inthesenseofcalculation)andtheprocedureexecutesabusinesslogic(althoughtechnicallythisisstillacomputation).

Convention

Languagesthatonlyhavefunctionsreturn"nothing" when only one procedure is desired, but they are still considered functions. Comparing:

procedimento teste()

is the same as

funçao teste() : nada //tipo do dado que será retornado, o Portugol não possui "nada"

What to do with each one

Each language or even each team can define what it can or can not do within a function or procedure. But this is usually in coding style and not strict mandatory rules for code validation.

It is common for the language to require that the procedure can only be called as a statement (command, something at the beginning of the line or via a specific command) and not as expression, usually requires a result that the procedure does not provide.

So if you apply to an algorithm defined abstractly you should consider the syntax of the representation used to know if there will be a differentiation over one or the other. Now that the question has a definition that is using Portugol I can say that it follows a little what Pascal does, which is a language that uses a little old technique.

For all intents and purposes understand well the function of the function that will know how the procedure should be. Unless you want to learn in a very academic way and only use pure functions and leave non-pure routines for procedures. I do not think it's worth distinguishing this in practical cases (I'm not saying that learning academically does not have its theoretical purpose).

Conclusion

Do not be afraid to find different definitions in different contexts.

Particularly I think these very abstract languages end up creating certain vices in the programmer because it's bound to do things that it would not do in more concrete language. What is the use of learning to create a procedure if almost all modern languages have only functions?

    
09.04.2016 / 04:52