Advanced programming question. How is this done?

Hello,

I'm an old timer is programming C or C++. I found this sample code for a webserver for Arduino. I do not understand how it works

In the my setup function they are adding server.on methods that is expanding the webserver code. How is this done? what is the name so I look it up and read more about it.

Here is the example code you will notice in the setup method to line that almost look like call to server.on but then I see , { server.send(200, "text/plain", "this works as well"); }

Is this new code or is there a method already define that takes ", { server.send(200, "text/plain", "this works as well"); }" as an argument?

I'm confused.
Thanks for any help

server.on("/inline", {
server.send(200, "text/plain", "this works as well");
});

#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char* ssid = "........";
const char* password = "........";

WebServer server(80);

const int led = 13;

void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!");
digitalWrite(led, 0);
}

void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}

void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}

server.on("/", handleRoot);

server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});

server.onNotFound(handleNotFound);

server.begin();
Serial.println("HTTP server started");
}

void loop(void) {
server.handleClient();
}```

Lambda expressions (since C++11)

Thanks that's makes sense.

mikep_1998:
Thanks that's makes sense.

The syntax seems really strange at first sight.

Maybe you missed some other features of the newer C++ versions too, could be worth checking them out. :wink: