Very basic problem, starting use of functions.

Hi all

Be easy on me. I'm new to this.
Just started using functions. Working from the book "Absolute C++"

I've written the code how I've been told to but keep getting this error -

error C2448: '<Unknown>' : function-style initializer appears to be a function definition

Heres the very basic test program I'm using. I've had to deviate from the book a little because I'm using Visual C++ 6.0 and it's complained about not initializing the variable seperately (hence the n1, n2, n3), and gave the above error for the following code, which I can't seem to get past...

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


int n1;
int n2;
int n3;
int sum(int n1, int n2, int n3);


int main()
{
	
	
	int sum(n1, n2, n3)
	{
		return ((n1 + n2) + n3);
	}

	cout << sum(7, 3, 80);
	


	return 0;
}



Its probably something very basic that I'm missing. I'd appreciate any help.

-greg
You function sum() needs to be outside main() which is itself a function. something like this...

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


int n1;
int n2;
int n3;
int sum(int n1, int n2, int n3);


int main()
{

	cout << sum(7, 3, 80);
	


	return 0;
}
int sum(int n1, int n2, int n3)
	{
		return ((n1 + n2) + n3);
	}

Last edited on
Thanks man. Sorted it out.
Last edited on
Topic archived. No new replies allowed.