Using the same function for different Serial ports.

I am looking for a way to run the same function using different Serial ports, for example:

void pump_command(int code, String command){
  String prefix="";
  prefix=prefix+"/"+code;
  command=prefix+command;
  Serial2.print(command);
  Serial2.write(13);
  delay(20);
  receive_serial();
}

and the same for Serial1, Serial 3 and so on. Surely, there is a better way to do this other than having separate functions that would be completely identical except for the port name, but I am at loss on what to google for. Could someone point me in the right direction, please.

You could pass the function the number of the serial port, and then use a series of if statements or switch statement.

void pump_command(int code, String command, int portNum)
{
  if(portNum == 0)
     Serial.print();
  else if(portNum == 1)
     Serial1.print();
  // etc.
}

Or, you could make the function take a reference to the HardwareSerial class.

void pump_command(int code, String command, HardwareSerial &thePort)
{
   thePort.print();
}

Thank you, off to read about pointers.

off to read about pointers.

Why? References are not pointers.

I think I saw where someone created an array with each of the serial connections in it

Then they were (IIRC) addressed by myArray[0] or myArray[1] etc

...R