user11174: I have an Arduino webserver that displays many buttons over a webpage (73 at the moment) that toggle different switches. I shortened up the button-drawing code with this function:
void displayButton(String toggleName, byte toggle, byte link1, EthernetClient & client, boolean last) {
  if (toggle == 0) client.print("<a href=\"/?");
  else client.print("<a class=\"linkSelected\" href=\"/?");
  client.print(link1);
  client.print("\">");
  client.print(toggleName);
  client.print("</a>");
  if (last) client.print("<br><br>");
}

And this is a shortened version of my loop function:
void loop() {
  EthernetClient client = server.available();
  if (client) {
    boolean done = false;
    while (client.connected() && !done)
    {
      while (client.available () > 0 && !done)
        done = processIncomingByte (client.read ());
    }  // end of while client connected
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();
      client.print("<HTML><HEAD>");
      displayButton("F8", f8Toggle, 57, client, 0);
      displayButton("F9", f9Toggle, 58, client, 0);
      displayButton("F10", f10Toggle, 59, client, 1);
      client.println("</center></body></html>");
      delay(5); client.stop();
  } // end of "if client"
} // end of loop

I'm wondering if there's any way to have the displayButton function use the EthernetClient instance without it having to be passed every time. I'm interested in this in order to clean up my code a little (since I would be able to remove 73 instances of the word "client"), and also as a learning experience.

user11174: As Majenko has noted in the comments, the solution was to declare "EthernetClient client;" as a global variable, outside of the loop. Normally I would've tried something like this on my own, but was hesitant to do so because I had never seen anything like that in other code.

11174: <p>I have an Arduino webserver that displays many buttons over a webpage (73 at the moment) that toggle different switches. I shortened up the button-drawing code with this function:</p>

<pre><code>void displayButton(String toggleName, byte toggle, byte link1, EthernetClient &amp; client, boolean last) {
  if (toggle == 0) client.print("&lt;a href=\"/?");
  else client.print("&lt;a class=\"linkSelected\" href=\"/?");
  client.print(link1);
  client.print("\"&gt;");
  client.print(toggleName);
  client.print("&lt;/a&gt;");
  if (last) client.print("&lt;br&gt;&lt;br&gt;");
}
</code></pre>

<p>And this is a shortened version of my loop function:</p>

<pre><code>void loop() {
  EthernetClient client = server.available();
  if (client) {
    boolean done = false;
    while (client.connected() &amp;&amp; !done)
    {
      while (client.available () &gt; 0 &amp;&amp; !done)
        done = processIncomingByte (client.read ());
    }  // end of while client connected
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();
      client.print("&lt;HTML&gt;&lt;HEAD&gt;");
      displayButton("F8", f8Toggle, 57, client, 0);
      displayButton("F9", f9Toggle, 58, client, 0);
      displayButton("F10", f10Toggle, 59, client, 1);
      client.println("&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;");
      delay(5); client.stop();
  } // end of "if client"
} // end of loop
</code></pre>

<p>I'm wondering if there's any way to have the displayButton function use the EthernetClient instance without it having to be passed every time. I'm interested in this in order to clean up my code a little (since I would be able to remove 73 instances of the word "client"), and also as a learning experience.</p>
 11174: <p>As Majenko has noted in the comments, the solution was to declare "EthernetClient client;" as a global variable, outside of the loop. Normally I would've tried something like this on my own, but was hesitant to do so because I had never seen anything like that in other code.</p>
