How to properly include functions written on other sketch tabs - revisited 2021

I read
How to properly include functions written on other sketch tabs

When I tried the method supplied of using multiple ino files by Tech-Guy it didn’t work.
(ignoring the need for serial.begin) This is what I tried

usingfiles.ino

int dothing(int);

void setup(){

   Serial.begin(9600); 
}

void loop() {

   Serial.println(dothing(1));                 
}

Usingfile2.ino

int dothing(int thing = 0){
     return thing;
}

The serial monitor should print zero as thing = 0 but it prints ones.

I’m not a fan of multiple .ino files. If you’re going to the trouble of using multiple source code files, then it’s much better to use the technique described in My Reply #3 to This Thread.

Strider222:
The serial monitor should print zero as thing = 0 but it prints ones.

This is wrong. The default value is set as 0, but you are passing a 1 to the function, which overrides the default:
https://www.cplusplus.com/doc/tutorial/functions/#defaultvalues
If you try this instead, you will get the default value as you were expecting:

  Serial.println(dothing());

But you'll need to specify the default parameter value in your function prototype instead of in the function definition, otherwise the sketch won't compile:

int dothing(int=0);

void setup(){

   Serial.begin(9600);
}

void loop() {

   Serial.println(dothing());                 
}

int dothing(int thing){
     return thing;
}

This has nothing to do with the use of multiple .ino file tabs. You'll get the same result if you put all the code on a single tab (or even if you do as @gfvalvo suggests).

@gfvalvo
It sounded great. I read your post you referred to and agree with proper programming methods.
I installed eclipse and got to the Sloeber install.

But...
"Sloeber sends non personalised activity data (build/upload/open serial monitor/open plotter) to baeyens.it for statistical analysis of the plugin usage."

I believe visual studio does the same.

I just don't agree with unauthorized monitoring. If I could turn that off then sure. I've installed eclipse but got stuck on that restriction. I don't want to live in a surveillance society. Maybe I'll give up some day but I can hope that if we all object.

Don't get your shorts all bunched up. The point of my post was the proper use of .cpp and .h files and what belongs it each. You can do that with the Arduino IDE rather than selling your soul to Eclipse.

Not all bunched up. Just disappointed. It looks good.

Your input was useful. Thanks. I have working CPP and H files. I added
#include <Arduino.h>
in the CPP file to provide the define for digitalWrite and delay.
I used the info in the latest
/tmp/arduino_build_#####/
from /tmp sorted by time to help.
That could come in handy for include libraries.

Had a couple of beginner mistakes; not saving before compiling and having multiple IDEs open to the same project. :confused:

Peace!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.