Hi
Please help me with the following queries to understand C++ conceptually. It would be really nice of you if you keep your explanation simple so that I can understand it.
1: What is a 'library function'? Is it the function included in the standard C++ library - i.e. the header #include <cstdlib>?
2: What is the difference between a function and function call?
3: Can I say the function "int main()" is the 'mother' function and all (or, most others) are its children functions such as sqrt()?
4: The book says:
Library Function exit()
This function causes the program to terminate, no matter where it is in the listing.
It has no return value. Its single argument, 0 in our example, is returned to the operating system when the program exits. (This value is useful in batch files, where you can use the ERRORLEVEL value to query the return value provided by exit(). The value 0 is normally used for a successful termination; other numbers indicate errors.)
What is "return value"? So far I have only seen int main() with which "return 0;" is used, and that too I have been told is optional.
Using exit(0), exit(1), exit(2),..., doesn't affect the working of the below given code but the above passage says only "0" value is used for successful termination.
5: The book says:
The last statement in the function body is return 0;. This tells main() to return the value 0 to whoever called it, in this case the operating system or compiler. In older versions of C++ you could give main() the return type of void and dispense with the return statement, but this is not considered correct in Standard C++.
I don't understand it. What is "void"?
Is return 0; a kind of signal at the end or somewhere else to tell that the job has been done and it's time to call it a day. Suppose if you have return 0; at the end of the code then the return 0; statement would only be reached when the execution of the code has successfully executed the above lying statements and once reached it tells job is done.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int n, i;
bool prime = true;
cout << "Enter the number: ";
cin >> n;
if (n == 1)
{
cout << "1 is neither considered a prime nor non-prime" << endl;
exit(0);
}
for (i=2; i<=(n/2);i++)
{
if (n%i != 0)
prime = true;
else if (n%i == 0)
prime = false;
{
if (prime == false)
break;
}
}
if (prime == true)
cout << "the number is prime" << endl;
else
cout << "not prime" << endl;
system("pause");
}