D
D
Dmitry2020-05-26 15:25:25
Programming
Dmitry, 2020-05-26 15:25:25

Are input and output parameters in the function body OK?

Although my field is PLC programming, but the question is "universal".

At the new place, I began to look at the texts of the programs and already timidly raised the question, but they told me - what's the matter, everything is fine.
I'll ask you too.

There are certain functions that are called in the program, functions have input, output (returned, input-output), internal variables... Well, sort of like in normal programming.
So, I have always believed that a function should only work with the variables that it received and return values ​​to those that are written outside.
I immediately see that once in the text of the function there is a call immediately directly to global variables and everything protests in me.

Who is right, is it written somewhere that this style is bad?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2020-05-26
@Maksclub

there is an appeal immediately directly to the global variables and everything protests in me.

Purity of function
In functional programming, functions tend to be pure, pure functions are simple, reliable, and easy to test.

D
Dmitry, 2020-05-27
@Mitya78

I repeat once again, maybe not everyone is watching in the comments:

spoiler
Может плохо выразился, но вот есть функция, которая условно при наличии двух дискретных входах ПЛК включает выход контроллера. Так я бы считал, что этот выход ПЛК должен быть привязан к выходу функции, а не устанавливаться внутри её.
Вот пример набросал:
// Вызов функции
ORGANIZATION_BLOCK "Main_1"
TITLE = "Main Program Sweep (Cycle)"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
BEGIN
"FC_Set_Q"(In1 := "In1_PLC",
In2 := "In2_PLC",
Out => "Out2_PLC"); // Верное обращение к выходу ПЛК
END_ORGANIZATION_BLOCK
// Сама функция
FUNCTION "FC_Set_Q" : Void
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
In1 : Bool;
In2 : Bool;
END_VAR
VAR_OUTPUT
Out : Bool;
END_VAR
BEGIN
IF #In1 AND #In2 THEN
#Out := TRUE; // Устанавливается выход функции, нормально
"Out1_PLC" := TRUE; // Устанавливается глобальная переменная, плохо
END_IF;
END_FUNCTION

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question