How Do I Create A Function Library In VEXcode Text?

I teach my middle school programmers the value of using functions to make their programming somewhat easier and more intuitive. We’ve learned that, in VEXcode Text, functions can be placed sequentially ahead of the main program (with global variables defined first at the top). This works very well, but creates a long list of code. In ROBOTC we could place the functions in a library and then “include” the library in our main program. We have not been able to create a library, or its equivalent, in VEXcode Text. I’d appreciate any assistance someone might have to offer.

1 Like

What you can do is create another file in your program to house all of your functions. If you go the the side bar with the files and right click, you can select “add file”

image

For example, in the program above, I have extra files to house functions for odometry, auton movement functions, and a function to draw the field on the Brain screen

5 Likes

Thank you for your prompt reply. I understand the source code for the functions in my proposed library will go in, for example, lib.cpp. What do I put in the corresponding lib.h? That is, what do you have in odom.h, for example? Also, where do I define global variables?

Your “main” definitions would go in .cpp. In the .h file, you want to write your “access code”. For example, if you had
int x = 0;
in your lib.cpp file and you want to be able to access this variable and use it in another .cpp or .h file, you would put
extern int x;
into your corresponding .h file. Just a side note, you can have 1 big .h file with all of your access code, but creating corresponding .h files is better for overall organization. Also, just because you create a variable in your lib.cpp file does not mean you need to include it into your lib.h file. Only include it into your lib.h file if you want to be able to use it in other files. And when you say global variables, I assume you mean those to be used in multiple files. So in that case, create your main variables in your lib.cpp (so just “normally”), write your access code in your lib.h file, and when you want to use it in another file, write
#include "lib.h"
at the top of the file you want to use the variables in. This line of code acts as an accessor, telling the computer that you are going to use variables already defined in another file.

E: Also, if you want to centralize all of your user created header files, you can include them all in vex.h which is already included in the main source file so this way your main source file only has the vex.h include.

8 Likes

Thank you – especially for your examples. I now have a program which uses a library that compiles without errors. Now all I need is a robot to test it, but that is another issue that is solvable once we get back to face-to-face practices. And that is expected to be soon. Thanks again for your assistance.

2 Likes