Need some help with my own function

Getting this error:

C:\Users\Michael\Documents\Arduino\8266Templ\8266Templ.ino:36:17: error: expected constructor, destructor, or type conversion before '(' token
   36 | setVariableValue("hostname", hostname);
      |                 ^

I'v searched the forum and google, and the problems they describe, i just don't see in my code.

The only thing odd i am doing is i wrote this code as a standalone sketch, and now just want to use those functions in another sketch.
Have commented out the problematic pieces that the IDE just is not allowing.

IDE 2.1.1, can't upgrade on a Windows 8.1 machine.

I've even attempted the "protyping" code, but it doesn't seem to help.

Main sketch, down to where it fails anyway:

// File name 8266Templ.ino

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include "ESPFlash.h"
#include "StringSplitter.h"

//  In separate .ino
// String substituteVariables(const String formatString);

void setVariableValue(String varName, String value);

#ifndef STASSID
#define STASSID "***"
#define STAPSK "***"
#endif

const char *ssid = STASSID;
const char *password = STAPSK;

String gethostname(bool full) {
  String result = __FILE__;
  if (full) {
    return result;
  } else {
    int idx = String(__FILE__).indexOf(".ino");
    if (idx > 0) {
      result = result.substring(0, idx);
    }
    return result;
  }
}

String hostname = gethostname(false);
setVariableValue("hostname", hostname);


And the actual function, from the other sketch:

void setVariableValue(String varName, String value) {
  int i = 0;
  int siz = sizeof(varArr) / sizeof(varStruct);
  varStruct tmpArr[] = {};

  if (getVariableValue(varName) == "") {
    if (siz != 0) {
      tmpArr[siz + 1] = {};
      for (; i < siz; i++) {
        tmpArr[i] = varArr[i];
      }
      // i++
    }
    tmpArr[i] = { varName, value };
    varArr[sizeof(tmpArr) / sizeof(varStruct)] = {};
    for (i = 0; i < sizeof(tmpArr) / sizeof(varStruct); ++i) {
      varArr[i] = tmpArr[i];
    }
  } else {
    for (i = 0; i < sizeof(varArr) / sizeof(varStruct); ++i) {
      if (varArr[i].name == varName) {
        varArr[i].value = value;
        break;
      }
    }
  }
}

Any help would be greatly appreciated.

You can't call a void function at top level in C or C++, you can only declare things (variables, functions, classes, structs etc). Put initialization code inside the setup() function.

Got it. Is that only with "void" functions ?
Because you'll see just above that, i have a function gethostname(), and i use it just below with:

String hostname = gethostname(false);

You should not call functions outside other functions.

At the top level, beginners use function definitions and global variables. Those variables can use initializer functions that return a value. There's no way to "just call a function".

Of course, any function can be made not-void

int do_something() {
  // whatever
  return 42;  // have to return a value, won't be used
}

int unused = do_something() + do_something_else() + this_is_a_bad_idea();

This is a bad idea. All initializers necessarily run before everything is initialized (except maybe for the last one). So you have to be careful not to depend upon or create side effects. This is worse when your sketch/program comprises more than one module. (All .ino files are glued together as a single .ino.cpp, but the sketch may also have other separate .cpp files. Libraries use separate .cpp files. Each is a module.) Initialization order between modules is undefined.

C/C++ has main, which with Arduino calls setup. This is where you should "just call a function" at the beginning.

Yep, got it @kenb4.
I actually went back and changed my sketch and place that "initialzer" function down below, just because looks better with "setup()" at the top, easier to find, lol.
Works as expected.
I only put it there because i was having an issue with it being down below, but got it all corrected.

Tnaks again, anybody / everybody.

If you ever write C/C++ code for a PC and use that approach your code will not compile :wink: Same when you advance in Arduino programming and start using .cpp files.

Just so you know.