My code has no mistakes(I think) but it still doesn't work as it should!

I'm a beginner so maybe I'm asking a dumb question but i can't sort it out so if you could help me a little it would be nice. Thanks in advance.

I was writing a program code but when i debug it (am using Visual Studio 2010 professional by the way) it doesn't print what i am expecting, only some random numbers come out( hexadecimal I guess) and there's no error message just those numbers .

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

int a,b;
int sum(int a, int b);

int main ()
{
	cout<<"Enter the firs number: " <<endl;
	cin >>a;
	cout<<"Enter the second number: "<<endl;
	cin>>b;
	cout<<"\n ";
	cout<< sum<<endl;

	return 0;

}


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


This is not the actual code that i was writing but this code prints also the same thing.
So if you could have a look at it and tell me am i making some kind of mistake or is this Visual Studios fault?.
Thanks.
Line 14, did you mean:
cout << sum( a, b ) << endl;?

Also, line 4. That doesn't need to be there. You declare the variables in main() and pass them to sum().
Actually, you don't declare them in main(). My bad.
Last edited on
You probably want to call the sum function on line 14:
cout<< sum(a, b) <<endl;
Thank you very much @Lynx876 and @ Peter87 .I didn't expect an answer so fast. It worked.
Like I said I'm a beginner and probably asking a dumb question :( . Thanks again.
Actually, you don't declare them in main().

But it would be better if a and b were declared in main.

Andy

PS And your orig code would have been printing out the memory address of the function sum()
Last edited on
@ andywestken. Thank you, like it seems I really can learn something here and I'll remember in such cases " it's better to declare variables in main". Thanks.
Topic archived. No new replies allowed.