Just started learning. I am using Arduino IDE 1.8.19. Mostly for ESP32 simple example projects. Now, I have CPP file that contains a function that returns two specific times of events; like sunrise and sunset time. This project I chose to learn using cpp in Arduino IDE. Could anyone please help me to learn how to call that function with arguments and get the returned values? I have very minimum experience in C++. Thank you
Welcome to the forum
Where did you get the .cpp file from and is there a corresponding .h file ?
Welcome to the forum.
It is hard for us to give advice when we don't know what you have.
If it is just a small function, then copy the function in your sketch and forget that *.cpp file.
You can rename the *.cpp file to a *.ino file and just call that function.
If you have also a *.h file with that *.cpp file, then don't rename it, but include the *.h file.
[ADDED]
If you have copied the *.cpp file from a library, then you can add that library.
This is a library: https://github.com/buelowp/sunset It can be added with the Arduino IDE, and then you don't have the *.cpp file in your project.
Pretty easy stuff... been explained several times over the years; but, it is a bit advanced for beginners to understand.
I discuss and have an example file here:
Just remember to adjust your #include for the .h if necessary.
Good luck,
Ray
when invoking a function, the compiler needs to know what arguments there are and what type of value it returns. in a .ino file, the compiler knows this from the "definition" of the function in the .ino.
when invoking a function external to the file, the compiler needs to see a "declaration"
extern int func (char c, float f);
note the semicolon and lack of a function body which are defined in the external file
rather than put the declaration in each file using the function, it can be put in a .h that is #included in the file calling the function and may include additional function declarations and possibly struct, enums, typedefs that may be used in the external .cpp file
The 'extern' is superfluous. Works the same with
int func (char c, float f);
yes it is, but it emphasizes the purpose for the OP
Thanks for your quick response. I got the .cpp and .h from internet.
Thank you very much. Thanks for your suggestions. I will read all the responses and follow the suggestions.
Thanks for the examples.
Thanks gcjr for the information. I will follow the steps
Thanks for this option. I will work on this next weekend
From where ? Can you give a link ?
Please see post##
Post number ?
Post #3
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.