-5

Original question

I am writing a function called Evaluate in C++. It accepts three arrays as parameters:

double Evaluate (double inputs[], double hidden[], double outputs[]) {
    // To-Do...
}

The problem appears in this scenario:

The programmer decides to initialize the function Evaluate with only two parameters: inputs[] and outputs.

So, I was thinking of creating Evaluate like this:

double Evaluate (double inputs[], double hidden[] = {}, double outputs[]) {
    // To-Do...
}

But, this creates strange Errors:

  In function 'double Evaluate (double*, double*, double*)'
34:53: error: unexpected '{' token
34:54: error: unexpected '}' token

Is there a solution?

*Thanks in advance.

Updated question

I have managed to use my answer with the help in the comments.

I am currently curious, won't multiple function overloads cause the program to get slower?

Ed The ''Pro''
  • 875
  • 10
  • 22
  • 1
    Replace all of those arguments with `std::vector`. – Hatted Rooster Jun 25 '18 at 11:29
  • 2
    Possible duplicate of [Make an array an optional parameter for a c++ function](https://stackoverflow.com/questions/37761243/make-an-array-an-optional-parameter-for-a-c-function) **Also note that optional parameters must come after mandatory parameters** – Nick is tired Jun 25 '18 at 11:30

1 Answers1

0

One way which I have learnt is function overloading - where you create copy of the same function, but in different ways.

int add(int a)
{
    return ++a;
}

int add(int a, int b)
{
    return a + b;
}

double add(double a, double b)
{
    return a + b;
}

This became so helpful that I'm able to implement many operations with it!

In terms of my evaluate function, I could do:

evaluate(std::vector<double> inputs, std::vector<double> outputs, std::vector<double> hidden)
{
    // ...
}

evaluate (std::vector<double> inputs, std::vector<double> outputs)
{
    // ...
}
Ed The ''Pro''
  • 875
  • 10
  • 22
  • What? How does this answer in any way relate to your original question? The fact remains that you cannot have non-default arguments appearing after default arguments, and you cannot default-initialize an array argument with `{}` – paddy Feb 22 '19 at 02:56
  • I have been testing function overloading to see if will help. The function evaluate would recieve three or two parameters. It there is no `hidden` parameter, then it would used the default hidden configuration. The example above is very minimal. – Ed The ''Pro'' Feb 22 '19 at 03:02
  • The point is that if you had instead defined your function as `double Evaluate(double inputs[], double outputs[], double hidden[] = nullptr)` you would have only required a single definition. – paddy Feb 22 '19 at 03:05