expected initializer

Feb 5, 2013 at 7:48pm
The folowing program will not compile. It says
5 14 C:\C++ stuff\test\main.cpp [Error] expected initializer before 'add'
Can anybody tell me what this means?

#include <iostream>

using namespace std;

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

int main()
{
cout << "Hello world!" << endl;
cout << add(2,3) << endl;
return 0;
}
Last edited on Feb 5, 2013 at 7:50pm
Feb 5, 2013 at 7:50pm
1
2
3
4
5
int add(int a, int b) //This should be the declaration for add. You can change the
//ints to float or whatever else you need.
{
//...
}
Feb 5, 2013 at 8:02pm
closed account (3qX21hU5)
In other words you need to have a type for your function parameters. Think of them as just like any other variable you need to define their types so you can use them. Just remmber the parameters are only availuble inside of the functions scope.
Feb 6, 2013 at 8:29am
Thank you for your answers, BlackSheep and Zereo. Unfortunately it did'nt solve the problem. The error message is the same:

5 14 C:\C++ stuff\test\main.cpp [Error] expected initializer before 'add'

Here comes the corrected program:

#include <iostream>

using namespace std;

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

int main()
{
cout << "Hello world!" << endl;
cout << add(2,3) << endl;
return 0;
}
Feb 6, 2013 at 12:46pm
closed account (3qX21hU5)
The problem is here int function add(int a,int b) You do not need function in there if you want the function to be named add just have this int add(int a,int b)
Feb 7, 2013 at 3:33pm
Hi Zereo.
Thank you very much.
Henning
Topic archived. No new replies allowed.