Simple function Problem

Why I get these errors when I try build this code?

C:\Codes\C++\C++ Projects\Test.c|7|error: initializer expression list treated as compound expression|
C:\Codes\C++\C++ Projects\Test.c|7|error: expected `,' or `;' before '{' token|
C:\Codes\C++\C++ Projects\Test.c||In function `int main()':|
C:\Codes\C++\C++ Projects\Test.c|18|error: `addition' cannot be used as a function|
||=== Build finished: 4 errors, 0 warnings ===|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int x1, y1, a, b, r2;

int addition (x1, y1)
{
    int r;
    r = x1 + y1;
    return (r);
}

int main ()
{
    cout << "Input a number a, b";
    cin >> a;
    cin >> b;
    r2 = addition (a, b);
    cout << r2;
    cin.get();
}

The function header has a parameter list without the parameter types.

Don't use unnecessary global variables.
Last edited on
So... I shoud declare them in their respective block? (Example: Declare a, b, r2 in int main())
Yes, do that. And on the addition(x1, y1), you need to define the types, i.e. addition(int x1, int y1).

You don't have to declare the variables you are using in functions as global variables, they are created when you call the function.
Last edited on
Ok, thanks!!!
Topic archived. No new replies allowed.