Program Structure and how to initiate Serial.begin in callable function

I'm trying to use a subprogram that will read the serial port. I can't figure out where to initialize the serial port without having to write it into the main program Setup function.

In the below pseudo code I've tried to show the crux of my question, though I know there are numerous syntax issues. I'm not good enough at programming to type this stuff off the top of my head and I've not written the actual code yet (I do have a bunch of snippets).

I guess I could put the serial setup code in the beginning of the subprogram with a header blocker type of setup. But I think there might be an approach I just can't see.

Any suggestions would be appreciated.

Thanks
John

Main program:

// My Program

// Globals etc
int x;

#include callablefunction.h



void setup(){

some setup stuff

} // --- end of setup ---

void loop () {

do this and that

 x = callablefunction
 
 } // --- end loop ---

Support code:

callablefunction.cpp

  static int count = 0;
  static int state = 0;

// other code
char inChar = (char)Serial.read() & 0x7F;
	
// more code

something like this you mean?

void func(HardwareSerial &refSer)
{
   refSer.print("ref to ser");
}


//calling func in loop
func(Serial);
void callablefunction(void)
{
  static bool init_finished;

  if ( ! init_finished )
  {
    Serial.begin( 250000 );
    init_finished = true;
  }

// other code
char inChar = (char)Serial.read() & 0x7F;
	
// more code 

}

Thanks guys :slight_smile:

I think I will use the suggestion by Coding Badly. It seems the most general.

The folks here are great, I've always received valuable advice here :slight_smile:

Thanks
John