Function and if else statement program help

Can someone tell me what I am doing wrong? I started to learn programming last week so i am still not that great at it. Thanks

//Function and If else program
#include <iostream>
using namespace std;

int func (int x, int y);

int main()
{
int x;
int y;

cout<<"In this program when x > y... x - y is executed\n"
<<"and when x < y... x * y is executed\n";
cout<<"Enter number for x: " << "\n";
cin>> x;
cout<<"Enter number for y: " << "\n";
cin>> y;

}


int func (int x, int y)
{

if (x > y)
{
cout<<"The difference is: "<<endl;
return x - y;
}

else
{
cout<<"The product is: "<<endl;
return x * y;
}

system("PAUSE");
return 0;
}
Well, to help the helpers tell us:
What does the program ACTUALLY do?
What is the program SUPPOSED to do?

Also, if you wanted to print the difference and product, you need to call func(x,y) in main.

PS: Hevitate using system("PAUSE") and any other system-like functions. They're not safe.
I just want this program to take int x and int y and the if int x is greater then int y i want it to subtract but if int x is less than int y i want it to multiply
basically if int x is greater than int y it should subtract and if int x is less than int y is should multiply
Change it the following way

//Function and If else program
#include <iostream>
using namespace std;

int func (int x, int y);

int main()
{
int x;
int y;

cout<<"In this program when x > y... x - y is executed\n"
<<"and when x < y... x * y is executed\n";
cout<<"Enter number for x: " << "\n";
cin>> x;
cout<<"Enter number for y: " << "\n";
cin>> y;

cout << func( x, y ) << endl;

system("PAUSE");
return 0;
}


int func (int x, int y)
{

if (x > y)
{
cout<<"The difference is: "<<endl;
return x - y;
}

else
{
cout<<"The product is: "<<endl;
return x * y;
}

}
Hi
At 1st, return 0 and system("PAUSE") must be in main() and the function func must come out of main, following it. Then, generally speaking, we define functions as subroutines that can be used to accomplish some task, however, definition of a function doesn't do anything on its own unless the function is called. So your program must be like this:

#include <iostream>
using namespace std;

int func (int x, int y);

int main()
{
int x;
int y;

cout<<"In this program when x > y... x - y is executed\n"
<<"and when x < y... x * y is executed\n";
cout<<"Enter number for x: " << "\n";
cin>> x;
cout<<"Enter number for y: " << "\n";
cin>> y;

cout<<func(x,y);

system("PAUSE");
return 0;
}


int func (int x, int y)
{

if (x > y)
{
cout<<"The difference is: "<<endl;
return x - y;
}

else
{
cout<<"The product is: "<<endl;
return x * y;
}
}

However, writing programs in this fashion is not as efficient as expected in professional programs.
Good luck
Topic archived. No new replies allowed.