Pass WiFi as parameter to library function

I am using WiFiNina and would like to move some of the setup and configuration code into a separate file, so I can modularise and reuse it. Is it possible to pass the WiFi object in the .ino to a function in a library with something like the below?

void WiFiSetup(WiFi myWiFi) {
}

Since there's no declaration of a WiFi object in the sketch, I'm not sure how to pass it, or if that can even be done.

Thanks!
John

The WiFi variable is a global object. Just #include <WiFiNINA.h> in the required file and you don't need to pass anything.

And if you want to know for the general case, you want to pass the parameter by reference (add the & before the parameter name) so that you don’t ask the compiler to build a copy and work on the copy. That’s usually a bad idea esp. when the instance deals with hardware resources,

void someSetup(MyClass& classInstance) {
   …
}

Ah, that makes sense. Thanks very much!

And thanks J-M-L for the tip about passing by reference.

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