Basic Function

Hi, I'm a newbie who just learn programming, my problem here is I just try to do a simple Addition program but the addition does not give a result but always give 0, I'm not sure which part I did wrong, can you please help me to debug the error since I not really understand about the function...The code below run on Borland C++, Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <conio>

int main(void)
{
   int a,b,c;

   void go();    //not sure whether this line correct?
   cin>>a;
   cin>>b;
   go();        //not sure whether this line correct?
   cout<<"The value c is \n"<<c;
   getch();
   return 0;
}

void go(void)
{
	int a, b, c;
   c=a+b;
}


but if no function, it will run well...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <conio>

int main(void)
{
   int a,b,c;
   cin>>a;
   cin>>b;
   c=a+b;
   cout<<"The value c is \n"<<c;

   getch();
   return 0;
}
Line 8 is wrong. You can't declare a function inside of function main.
Last edited on
On a technicality - line 8 isn't wrong - it is just unusual - you can declare a function (prototype) inside another function - however you can't define a function inside another function.


The real problem is that the variables a, b and c inside main function - are not the same as the variables inside the go function.

He needs to go and read up on functions, and variables and how to pass variables to functions, etc, etc..
Last edited on
Topic archived. No new replies allowed.