How to pass 'Serial' as an input argument in Arduino function?
The Serial object is the end of a chain of classes, each with a different set of functionality.

Print -&gt; Stream -&gt; HardwareSerial =&gt; [Serial]


Since you are reading from it the best class to use is Stream since that is the one that is the one that contains the reading functionality.

You should pass it by reference or as a pointer since you don't want to copy the object.

int compareFunction (int d, Stream &amp;Serialx)
{

  int ZZ = Serialx.readString(); // I will receive some values from motor using serial4
  //to avoid long code I deleted unnecessary parts
  // I will use ZZ and d values and output will be yy
  return yy;
}


By using Stream you can use any object type that also uses Stream in its class inheritance chain - even things like a WiFi socket connection.

This behaviour is called polymorphism and is one of the more powerful features of classes in C++ (and other OO languages).