user11174: I have an arduino webserver that uses the following code:
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println();
client.println(F("<HTML><HEAD>"));
...
if (fan1Toggle == 0)
    client.print(F("Fan1<font color=\"red\">&#8226;</font><a href=\"/?fan1on\">On</a><br>"));
else
    client.print(F("Fan1<font color=\"green\">&#8226;</font><a href=\"/?fan1off\">Off</a><br>"));

if (fan2Toggle == 0)
    client.print(F("Fan2<font color=\"red\">&#8226;</font><a href=\"/?fan2on\">On</a><br>"));
else
    client.print(F("Fan2<font color=\"green\">&#8226;</font><a href=\"/?fan2off\">Off</a><br>"));
...

The page is pretty long, allowing the user to toggle over a dozen different switches. Some of the code checks to see the state of a certain switch, and then displays a green or red bullet ("&#8226", like •) on the page if the switch is on or off.
Since the code for the colored bullet is repeating, I wanted to make a function for it. Then I would just call (for example) "displayBullet(fan1Toggle);", and it would determine if fan1 was on or off, and display the correct color depending on the state.
I tried to create the following function:
void displayBullet(int toggle) {
if (toggle == 0)     //switch is off, so display red bullet
    client.print(F("<font color=\"red\">&#8226;</font>"));
else    //switch is on, so display green bullet
    client.print(F("<font color=\"green\">&#8226;</font>"));
}

But then I got an error saying that "'client' is not declared in this scope".
Is there any way to work around that error, so that I can get the displayBullet() function I want? Or is this something that can't be shortened like this, because the "client" function has to be inside the main loop?
Thanks!

user10794: You can pass client to the function by reference, like this:
void displayBullet(EthernetClient & client, int toggle) 
  {
  if (toggle == 0)     //switch is off, so display red bullet
    client.print(F("<font color=\"red\">&#8226;</font>"));
  else    //switch is on, so display green bullet
    client.print(F("<font color=\"green\">&#8226;</font>"));
  }

Now it knows about client. Make sure you pass client when you call it:
displayBullet(client, 1);


11174: <p>I have an arduino webserver that uses the following code:</p>

<pre><code>client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println();
client.println(F("&lt;HTML&gt;&lt;HEAD&gt;"));
...
if (fan1Toggle == 0)
    client.print(F("Fan1&lt;font color=\"red\"&gt;&amp;#8226;&lt;/font&gt;&lt;a href=\"/?fan1on\"&gt;On&lt;/a&gt;&lt;br&gt;"));
else
    client.print(F("Fan1&lt;font color=\"green\"&gt;&amp;#8226;&lt;/font&gt;&lt;a href=\"/?fan1off\"&gt;Off&lt;/a&gt;&lt;br&gt;"));

if (fan2Toggle == 0)
    client.print(F("Fan2&lt;font color=\"red\"&gt;&amp;#8226;&lt;/font&gt;&lt;a href=\"/?fan2on\"&gt;On&lt;/a&gt;&lt;br&gt;"));
else
    client.print(F("Fan2&lt;font color=\"green\"&gt;&amp;#8226;&lt;/font&gt;&lt;a href=\"/?fan2off\"&gt;Off&lt;/a&gt;&lt;br&gt;"));
...
</code></pre>

<p>The page is pretty long, allowing the user to toggle over a dozen different switches. Some of the code checks to see the state of a certain switch, and then displays a green or red bullet ("&amp;#8226", like &#8226;) on the page if the switch is on or off.</p>

<p>Since the code for the colored bullet is repeating, I wanted to make a function for it. Then I would just call (for example) "<code>displayBullet(fan1Toggle);</code>", and it would determine if fan1 was on or off, and display the correct color depending on the state.</p>

<p>I tried to create the following function:</p>

<pre><code>void displayBullet(int toggle) {
if (toggle == 0)     //switch is off, so display red bullet
    client.print(F("&lt;font color=\"red\"&gt;&amp;#8226;&lt;/font&gt;"));
else    //switch is on, so display green bullet
    client.print(F("&lt;font color=\"green\"&gt;&amp;#8226;&lt;/font&gt;"));
}
</code></pre>

<p>But then I got an error saying that "'client' is not declared in this scope".</p>

<p>Is there any way to work around that error, so that I can get the displayBullet() function I want? Or is this something that can't be shortened like this, because the "client" function has to be inside the main loop?</p>

<p>Thanks!</p>
 10794: 

<p>You can pass client to the function by reference, like this:</p>

<pre class="lang-C++ prettyprint-override"><code>void displayBullet(EthernetClient &amp; client, int toggle) 
  {
  if (toggle == 0)     //switch is off, so display red bullet
    client.print(F("&lt;font color=\"red\"&gt;&amp;#8226;&lt;/font&gt;"));
  else    //switch is on, so display green bullet
    client.print(F("&lt;font color=\"green\"&gt;&amp;#8226;&lt;/font&gt;"));
  }
</code></pre>

<p>Now it knows about <code>client</code>. Make sure you pass client when you call it:</p>

<pre class="lang-C++ prettyprint-override"><code>displayBullet(client, 1);
</code></pre>
