Function Prototype

#include<iostream>
using namespace std;

int welcome()
{
cout<<"\t\taggregation programn";
cout<<"2 integers "<<endl;
}

int result(int ,int )
{
int x, y;
return(x+y);
}

void input_value()
{

int x, y;
cout<<"first number :" ;
cin>>x;
cout<<"second number :" ;
cin>>y;
}

int main()
{
int x, y;
welcome();
input_value();

result(x,y);
cout<<x<<"+"<<y<<"="<<result(x,y);
}

why the program error? where mistakes?
somebody please help me
Last edited on
Did you try compiling it? The compiler will tell you if there is an error and if so, where it is.
Your welome() function thinks it should return an integer, but it doesn't. If it's just going to print out statements, it can be a void function instead of int.

Also - the x and y variables you define in each function are local to that particular function. You are passing all variables by value, not by reference, so changes one function does to x or y will not be reflected in another function. So in the main function when you pass x and y to the result function, you are sending uninitialized values.

You don't need to call result(x,y) separately before the cout statement at the end of main.

Edit: Scroll down on this page to the "Arguments passed by value and by reference" section for more info on passing variables by value vs. by reference.
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
program can be run, it's just that the results are wrong. The program does not add up properly
i dont understand about 'reference' that you mean, please correct at program script
The program does not add up properly

That's because the x and y in result and input_value are not the same x and y in main.

You can fix your result function by actually using the two parameters you gave it:
1
2
3
4
int result(int x, int y)
{
    return x+y;
}

Now, for your input_value function, you need the x and y in that function to be the same x and y that you declared in main (right now, they're completely separate from each other).
One way to do that is to pass them by reference:
1
2
3
4
5
void input_value(int& x, int& y)
{
    cout << "first number: ";
    cin >> x;
    // ... 
Then, to call input_value, you would write input_value(x, y); in main.

On a side note, your welcome function says that it returns an int, but it doesn't.
Also, you don't need to call result twice -- the first call doesn't magically "store" the result anywhere or anything like that.
Last edited on
thankyou
Topic archived. No new replies allowed.