How to use SWITCH?

I have a routine in a .cpp file that sets a variable based on a condition.

I then want that condition to run a routine in another .cpp file when the previous routine exits.

I am trying to use the switch{} function to do this.
The error is that the routine is not declared for each case.
I have switch in a file with another void and the necessary declarations are in an included file outside the void but only the void sees them.

How can I make the switch function see them?

If I try to include that file in each case, I get other more difficult to describe errors from the called file.

Without the switch function everything compiles

Get your words right. C++ has functions, not voids nor routines,

If the function you want to call is in another .cpp file, you need to declare it with extern in the file where you want to use it (or create a .h with all the extern and include that .h)

extern void myFunction(int);
…
  // you can call it in the code later
  MyFunctions(3);

And in the other file

void myFunction(int value) {
  // your code here
}

This has nothing to do with switch(), you should change the title of your first post

I can't follow what you are saying. Please post all of your sketch and maybe we can help.

switch is not a function, its a statement.

There's no way to help if you don't post your code and explain what its doing wrong (different from your expectations).

I'm 79 and just starting to figure this out. I'm still speaking Fortran. Excuse me. But you seem to understand so kudos to you.

Well SWITCH is the "STATEMENT" I am having problems with.

I have 20 of these. It just seems like there should be an easier way to do it.
So I declare the function as extern, then I can call it inside the CASE:, right?
What is (int) for? Is that just if I want to put the same name on all the "functions" with just a number to diff?

please post the files using </>

the int was an example, just to show how you define parameters

to simplify switch only works on numbers (integral or similar types), so if you have a String or something, it won't work.

assuming you have a separate .cpp file, with some functions

void dothis() { ... code goes here ... .} 
void doThat() { ... code goes here ... .} 
void doSomethingElseWithX(int X) { ... code goes here ... .}

then in your .ino (assuming this is where you want to use those) you'd go

// let the compiler know that those function exist and what they look like
extern void dothis() ;
extern void doThat() ;
extern void doSomethingElseWithX(int);

void setup() {...}

void loop() {
...

  int value = getDataFromUser(); // wherever you get the data from
  switch (value) {
    case 1: dothis(); break;
    case 2: doThat(); break;
    default: doSomethingElseWithX(value); break;
  }
...
}

Helicopter view = What happens is your files get compiled separately, so the compiler needs to know what your function look like (does it have parameters? does it return something? ...) so that it can generate some placeholder code and at the final phase of compilation, the linker puts everything together and connect the real function to the placeholder.

does this help?

PS: do yourself a favour and please read How to get the best out of this forum and post accordingly (including code tags and necessary documentation of your ask). You'll get better answers, faster.

Some resources:

https://www.arduino.cc/reference/en/

https://www.cprogramming.com/tutorial/c++-tutorial.html?inl=nv

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

I put the SWITCH function into the file that contained the variable and it compiles.
Just wondering if there is a way to put it into its own file for my sanity reasons?

I had made the variable global using extern, but apparently that doesn't work.

???

????

please post code as stefan has asked

It works the way it is with the code in the same file as the switch(variable). It says in the lit that one cannot use a global variable. The question now is, is that true or is there a way to use a global in a switch?
I would gladly post the code if I thought it would help at all but based on what I have found so far, it won't. And if I do, it is multiple files and there are lots of complaints about TOOOOOOO much code posted.

Post all your code and all your compiler errors, verbatim, in code tags please, otherwise we'll all be chasing our tails fruitlessly... A minimum example showing the problem can be very useful if the files are large.

And as I said switch is a statement, not a function. These words have precise meanings and if you don't understand the difference it will be well worth learning up as many more things may become clearer.

Not sure where you read otherwise (throw that away) but You can use a global variable in a switch().

If that variable is unknown to the file Being compiled though the compiler will bark at you.

Declaring the variable as extern will solve that.

Agree with the others if you want more info, post a minimal example.

This may help

I think since the start of this thread OP’s challenge is not with the code itself but with separate compilation units and how to cross declare functions and variables

Reading about the preprocessor, compiler and linker would help understand what’s going on.

thanks JML
I am a beginner with this and I am trying to modify someone else's project. Actually, at this point I am rewriting it as my idea is much more extensive. What I am finding is exactly what you say. Most of the errors that I get about the code I can resolve. It is the ones that involve the structure that are baffling me. I think I just realized what the diff was between an .h file and a .cpp file, though am not sure it has fully sunk in.
As far as posting code, If I knew what area the problem was in, I would definitely post it, but the errors I was getting did not lead me to anything to focus on. As it was, I needed to change a file to a .cpp and not include it in other files. It didn't have anything to do with the function it was saying was multiply defined.
The switch function uses a variable that I thought was globally defined. I am sure it is but unless I put the function in the file that defines it, it gives an error that relates to something I found that said I couldn't use a global variable. Yes, there is lots of miss info out there and this may be one of them. Or it may be written so I don't understand it.

I have uCable declared as extern in an .h file
extern int uCable;
I include that .h file in a .cpp file that sets it to 0 and calculates the final value.
Then I had another .cpp file with the switch function in it
switch(uCable)
{........
and it said uCable was undefined, but I had the .h file included in that .cpp also.
I don't remember where I read that I couldn't use a global in a switch function, or why, but now the function is in the file that calculates uCable and it is happy.
I was just trying to keep things separate to help me navigate this growing program.

The actual switch function is not the problem. I think I got that.
Connecting the dots is.
At this point the program compiles, but it is far from finished. I may try to move the switch to its own file again but much later.
I have been focused on functions, variable, operators, and such and actually never thought about reading about preprocessors, compilers, and linkers. So I am off to that.
Thanks for understanding.