expected initializer

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
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.
{
//...
}
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.
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;
}
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)
Hi Zereo.
Thank you very much.
Henning
Topic archived. No new replies allowed.