What is the difference between functions and procedures?

Asked

Viewed 7,950 times

10

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 code with algorithm in Portugol.

  • 2

    It’s a kind of "nut" explanation, but just to start the subject: in most contexts, the function returns a value, and the procedure does not. Actually, it depends a lot on the context. Ideally, procedures would perform step-by-step tasks, and functions would simply do data processing and return a return, without generating side effects, but in imperative languages this side effect thing gets kind of relativized.

  • I have not studied any programming language, I am still in the course of algorithms in English.

  • 1

    possible duplicate in http://answall.com/questions/11848/quais-s%C3%A3o-as-defini%C3%A7%C3%B5es-de-m%C3%A9todo-fun%C3%A7%C3%A3o-e-procedimento

  • @Bacco I agree with you ^^

1 answer

14


Muddle

Many experienced programmers may have difficulty understanding the difference between one and the other. They confuse so much that some people think that functional paradigm languages are the languages that have functions and the procedural paradigm languages are the ones that have procedures. Which is a complete nonsense, because deep down both are (almost) the same thing.

Deep down 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 in 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 essence both the two cited in the question and methods work in the same way and concretely the implementation is essentially the same.

What differs

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

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

Pure function

Since the term "function" comes from mathematics it should only perform a calculation and return the result without doing extra operations, without doing input and output, without changing state outside of its execution (only moves local variables, created within the function), in short, the function should be pure (deterministic, with no side effects). Only a few functional languages really require purity. The practice of purity is impractical for most of the problems commonly encountered in computing. Fucntion In other words, the function computes something (in the sense of calculation) and the procedure executes a business logic (although technically this is still a computation).

Convention

Languages that only have functions return "nothing" when you want only one procedure, 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

Each language or even each team can define what they can or cannot do within a function or procedure, but this usually stays in coding style and not mandatory rigid rules for code validity.

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

So if applying to an abstractly defined algorithm one 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 functioning of the function which will know how the procedure should be. Unless you want to learn very academically and only use pure functions and leave no neat routines for procedures. I don’t think it’s worth distinguishing this in practical cases (I’m not saying that learning academically does not have its theoretical purpose).

Completion

Don’t be scared if you find different definitions in different contexts.

Particularly I think that these very abstract languages end up creating certain addictions in the programmer because he is obliged to do things that he would not do in more concrete language. What good is learning to create a procedure if almost all modern languages only have functions?

Browser other questions tagged

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