General questions on functions

Hello guys,
I have some rather general questions on C/C++:

  1. Say I have some (legacy) code which uses a function called "whackadoodle" and I would like to know what that function does. As far as I comprehend, this is a rather tedious task since I need to dig through potentially every file in my "include folders" to eventually find the needed definition. This is ok for small programs, but as complexity increases (and thus more and more functions are needed), this can become a real torment. So how do "pros" deal with this issue? Do IDEs have some sort of "function finding tool"? (Arduino IDE for instance does not seem to have one)

  2. Is it advisable/best practice to include the header file of a function, class etc. in the definition file of that same function/class?

  3. To be able to create the first basic functions, there obviously must be some C commands which the compiler will understand unconditionally (i. e. without including any libraries). Is there a list of those?

Thank you a lot.

  1. Grep the files for the name of the function with a return type in front. Or use a real grown up IDE like eclipse. There when you hover over a function name it gives you a little window with the definition and you can click it to open that file.

  2. No idea what you mean

  3. Look at the C or C++ reference. Do a tutorial on C or C++. This is not esoteric knowledge. It’s everywhere.

As far as I comprehend, this is a rather tedious task since I need to dig through potentially every file in my "include folders" to eventually find the needed definition.

If the function is not part of the main function then it will either be in one of the libraries explicitly #included by the program or be a system function included behind the scenes. To find functions in #included libraries look for the folder of the same name within the libraries folder of your sketch directory and look at the .h and .cpp files.

Thank you for your replys.

  1. Well, Arduino does not seem to support grep and even if it did, you can (AFAIK) not use the IDE to run commands on the PC (it is only for compiling code and sending it to the Arduino which will run it, so you would need to create a grep sketch and print the results to the serial monitor). Apart from that, using grep seems to do pretty much the same thing a search tool would do, so it wouldn't really speed up the process.

  2. What I mean is: To my knowledge, functions are usually not defined within their declaring header file (which is only for declaration), but in a separate source file. Though not necessary, you could include that header in that source file. Is that a common thing/best practice to do?

  3. I actually have done a youtube tutorial on C++ and also have tried to google the question, but so far I have found no specific answer to my question. I know you can google any library or command, but what I would like to know (out of mere curiosity) is which commands are truly "elementary" in C (and not introduced through libraries explicitly or behind the scenes). Can you post a link?

I know I'm not a pro, which is why I'm asking.

Greetings and thanks.

You run grep on the host PC, because that’s where the source files are.

If you're running on a PC, Windows File Manager has a search function that will find all references to any function for you....

Regards,
Ray L.

To be able to create the first basic functions, there obviously must be some C commands which the compiler will understand unconditionally

The first line of the function code defines the name of the function, the type of value it returns (if any) and the type and values of any arguments ("void" means none in either case). For example, your function "whackadoodle" might take one float and return another:

float whackadoodle(float input) {
return 2.0*input;
}
  1. Of course you don’t grep through the IDE. Don’t let’s be silly. Run grep on the PC to search the files in the core where you installed Arduino.

  2. You have to include the header where you define the functions or you end up with errors.

  3. I’m not sure what’s confusing. The stuff that is part of C is part of C. Like while and for and if and all that stuff you learn in basic C. What’s C++ is C++ same way. Everything else is from a library of some sort. Even the standard C++ library stuff like sprintf and atoi.

Forget YouTube. Google search C++ tutorial and there are several great ones that will be at the top of the list. Look through that to get a reference for what C and C++ can do.

This query presumably arose out of a desire to know what a particular function does. Can you please give an example or examples of such functions ?

able_archer_83:

  1. Say I have some (legacy) code which uses a function called "whackadoodle" and I would like to know what that function does. As far as I comprehend, this is a rather tedious task since I need to dig through potentially every file in my "include folders" to eventually find the needed definition. This is ok for small programs, but as complexity increases (and thus more and more functions are needed), this can become a real torment. So how do "pros" deal with this issue? Do IDEs have some sort of "function finding tool"? (Arduino IDE for instance does not seem to have one)

The Ardino IDE is very bare-bones and targeted at beginners. As far as I'm concerned, it's unusable for serious software development. If you want "pro" features, you need to use a "pro" IDE. I'm using Eclipse / Sloeber. However, I believe there are other options.

One quick but sloppy way to find which file a function is located in would be to add some extra argument to the function call, that would be unlikely to ever be a legitimate argument, and the warning or error message generated by the compiler will tell you the file that the closest matching function is located in.

Do IDEs have some sort of "function finding tool"?

Yes. Even Arduino has “search in all tabs”, “real IDEs” will usually have a “go to definition” feature. “Tags” and “ID tools” are utilities that build up a fast database usable by “legacy editors” and from the CLI. And I’ve used at least one “code browser” that came with vendor code that wasn’t part of an IDE (with STM Cube, iirc)

The ID Tools are especially impressive. Creating the database for an extremely large code base (1000s of files) can take a few minutes, but searches thereafter are very quick...