Jump to content

Using STRUCTS organizing variables


Recommended Posts

Hello everyone, I would like to know your opinion and advices of my procedure to organize my set of variables. I'm developing a program for a machine with many functions. I divide the machine in 10 different functions, each of them is related with one single process. In order to reduce and organize a lot of different variables, (more of them with same or similar name), I decide to create a single STRUCT for each function. These structs contain the variable that I need for this specific function.

For example, the "MIXER" is an STRUCT with the following variables: Auto, Enable, End, Alarm, TempLow, TempHigh, Running, etc. 

So, for example: When I need to use one of these variables, I just call MIXER_1.Auto, instead of the old way that I use with global variables Mixer1_Auto, and also I can reuse the STRUCT if I have more than one same functions: MIXER_2.Auto 

My goal is to keep the list of variables as low as I can, but I'm not sure is this is a good idea, o I'm losing other fantastic way to keep the variables well organized.

I know that each function can use Function_In and Function_Out variables, but I think that this way to program is worse because I need to double the number of variables. The ones used inside the function, and then the globals that I need to connect to the function when I call it.

So, I'm doing something logic or I need to change my way of programming?

I hope you understand me... thank you! 

Link to comment
Share on other sites

Hi GermanM,

You HAVE to use struct's !

When calling a function, you don't use every variable of that struct as seperate Function_in, but only the struct itself...


when you change something of the data of your "input" struct variable (like in my case here "hgrp 2 PR spots") after quitting the function, the values are changed also

my struct has several ints, bits, reals, text.....

I call the function call scene check with the variable struct hgrp 2... , do something in that function, like setting a bit, changing text or whatever

after calling the function, the bit's are set, text has changed....


advantage of using structs -> you can use them in data tables.... easy for debugging, just make a screen of that table and you see all variables of your structs (same structs of course)




Link to comment
Share on other sites

Hi Pascal, thank you for your answer!

I understand your POV my concern is that I don't see the advantage of declare the "Function_In variable" when I program a function. I just use a global custom made Struct for each function with all the variables that I need, so when one of these variables changes I can use it directly in any part of my project.

Well, I think that could be a good idea, some tips from Unitronics with good programming practices managing medium/big projects. How to organize the set of variables, Is good idea use the IO tag name of input directly? These types of things... I suppose that I'm just a newbie!!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...