#include <iostream>
#include <conio.h> // Not <conio>
usingnamespace std; // You forgot this
void is_even(int a); //Function prototype
int main(void)
{
int a;
cout<<"Enter an integer:\n";
cin>>a;
cout<<"Is "<<a<<" even? ";
is_even (a);
cout<<endl;
getch();
return 0;
}
void is_even(int a) // You needed to make this a void function
{
if(a%2==0) // should be %, not /
{
cout<<"Yes";
}
else //What about the other case?
cout << "No"; // What about the other case?
}
In general it is a bad idea to forse a function to perform two or more tasks. In case of your function it performs two task: determinates whether a number is even and sends some data in output stream. It would be better to separate these tasks.
For example you could define your function as
1 2 3 4
inlinebool is_even( int x )
{
return ( x % 2 == 0 );
}
#include <iostream>
usingnamespace std;
bool is_even(int a);
int main(void)
{
int a;
cout<<"Enter an integer:";
cin>>a;
if (is_even(a))
cout << a << " is even" << endl;
else
cout << a << " is not even" << endl;
return 0;
}
bool is_even(int a)
{
if(a%2) returnfalse;
returntrue;
}
That can be reduced further to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
bool is_even(int a);
int main(void)
{
int a;
cout<<"Enter an integer:";
cin>>a;
cout<< a << " is " << (is_even(a) ? "" : "not") << " even" << endl;
return 0;
}
bool is_even(int a) // You needed to make this a void function
{
if(a%2) returnfalse;
returntrue;
}
Enter an integer:3
3 is not even
Press any key to continue . . .
No I did not say that function are not allowed to perform more than one task. I said that it is better when you code consists from simple fucntions with clear logic.
To demonstrate that in case of the original example it is better that function is_even perform only one task consider situation that you need not to display string literals "yes" or "no" but you need to determine whether a value is even or odd in conditional statement. For example, assume that you need to calculate the sum of even values of some array. In this case you could write very simple code
1 2 3 4 5 6 7 8 9 10 11
int a[] = { 1, 5, 4, 7, 8, 5, 4, 9, 2, 5 };
int odd_sum = 0, even_sum = 0;
for ( int i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
if ( is_even( a[i] ) ) even_sum += a[i];
else odd_sum += a[i];
}
std::cout << "sum of even values is " << even_sum << std::endl;
std::cout << "sum of odd values is " << odd_sum << std::endl;
So if your function perform only one task it becomes more universal and its logic is very clear for its uses.
atjm88,
you are wrong. It is not my code that you cannot compile. It is your code that you can not compile. Please look one more what code I suggested.
Thanks..Is that means function are not allow to perform more then 1 task? May I know the reason? TQ...
It's called cohesion. To perform one task and perform it well. It has a number of benefits such as the function is more general (more reusable), easier to test, contains less logic (easier to understand), etc..
Thanks...
For vlad, I tried to run in VSC++ 2010 but still cannot...
This is the error...
1>c:\users\128a5s5\documents\visual studio 2010\projects\22\22\22.cpp(20): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>22.obj : error LNK2019: unresolved external symbol "bool __cdecl is_even(int)" (?is_even@@YA_NH@Z) referenced in function _main
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>c:\users\128a5s5\documents\visual studio 2010\Projects\22\Debug\22.exe : fatal error LNK1120: 2 unresolved externals