T
T
The_frog2021-11-05 13:17:35
Pascal
The_frog, 2021-11-05 13:17:35

What is the difference between functions, procedures and operators (on the example of FreePascal)?

Literature: Stolyarov, "Programming, an introduction to the profession"

Procedures:
61850380ddbc9284635986.png
618503a189443977937495.png

Operators:
618503bccf1bc040128234.png

Functions:
618503db6e066579025859.png

An example of using this all:
61850426904a0096326481.png
6185055ab9cfd445490724.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2021-11-05
@The_frog

Functions and procedures are almost the same thing. The only difference is that a function returns a value, while a procedure does not. Obviously, a function can be used like a procedure by simply ignoring the return value. Therefore, in other languages, such a concept as a procedure is usually not even introduced. And in C-like languages, where a function must have a return type, the void type is used, which also kind of makes the function a procedure.
Speaking of functions or procedures, the emphasis is on the calling mechanism itself - the pointer to the current execution location is placed on the stack, control is transferred to the function, and when it ends, the pointer is restored from the stack and program execution continues from the same place. Thus, two points are meant: 1) the function call itself 2) the location of the function body in another memory location. That is, a function is essentially a subroutine.
The operator is, in fact, a separate independent instruction. Usually they can be interchanged at the same nesting level, and this will not add syntax errors. However, the instruction itself can be not only simple, but also complex, compound. For example, a whole block, or a conditional statement, or a loop. Thus, an operator can contain other operators. This must be understood in order not to get confused in the terminology. Speaking of operators, the emphasis is on the fact that code is a sequence of instructions that are executed one after another. A function call (its call) is also an operator in the sense that it is a separate instruction.

O
OCTAGRAM, 2021-11-05
@OCTAGRAM

Procedures do not return a result, functions do.

$X (eXtended syntax)
Эта разница осложняется тем, что Паскаль после Вирта чаще портят, чем делают лучше. И, например, сделали возможным выбрасывать результат функции и вызывать функцию как процедуру. В Delphi это регулируется директивой $X (eXtended syntax), который паровозом меняет сразу много всего, и нельзя по отдельности только запретить выбрасывать результат. Паровозом становится нельзя образаться к результату по имени переменной Result. Казалось бы, можно пользоваться Exit(значение), но компилятор начинает глючить, когда тип значения record с управлением владением. И буфер символов array[0 .. 1023] of Char становится сложнее превратить в строку. Также становится обязательно использовать каретку для разыменования указателей. Смешались в кучу кони, люди. Куча синтаксических отличий переключается одной директивой, и сделано так, что с выключенной директивой толком жить не получается. Может быть, в FPC иначе, там свои глюки.

As for the operators, in Russian there is a confusion of translations of words that are very different in English. Good translators have resolved this confusion by saying that statement = operator and operator = operation. But not all translators agreed on the glossary.
A statement is:
  • empty statement
  • assignment
  • procedure call
  • compound statement begin statement; operator; … end;
  • other statements like goto, raise, try, if, for, while, repeat

An operation (operator) is:
  • addition, subtraction, multiplication, division
  • comparisons
  • logical and bitwise operations and, xor, or, not
  • bit shifts shl, shr
  • explicit and implicit type conversions

Delphi 2006 and later uses the class operator syntax to declare your own operators. Free Pascal also had its own operator syntax.
There can be confusion here, because in Pascal not all operators are honest operators, as they should be. For example, Exit, Break, and Continue are not syntactically simple statements, but are procedure call statements, and these special procedures are located in the System module, so the full name System.Exit will also work.
The author of the tutorial probably thinks that Read and ReadLn, on the other hand, are simple operators. But no. These are procedures from the System module, which are hung with compiler magic so that they take different parameters, but this magic does not make them statements. Although a procedure call is formally an operator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question