First of all, your function should return a bool
instead of an int
:
bool isPrime(int number) ...
Next, you need to tell cout
that you want to print boolean values as true
or false
instead of 1
or 0
. This can be done with std::boolalpha
:
std::cout << std::boolalpha << isPrime(number);
You have to #include <ios>
for that.
Edit
To answer some questions from the comments:
What's the issue if the function is of type int and not bool?
The whole point of a type system is to express and restrict the behavior of a program. If you call a function you better know what you pass into that function and what that function returns. Types help you with that.
Take your isPrime
function as an example. Let's say you didn't write that function but someone else did and you just use it as a library. You see the function signature int isPrime(int);
. The function takes an int
as an argument and you can probably guess what that argument has to be: the number that you want to check.
Now look at the return type int
. What does this mean? The function name isPrime
indicates that the function answers a simple yes/no question: is the given number a prime number? However, an int
can represent more than just yes
or no
. For example, what does it mean if that function returns 2
or -1000
(remember: you didn't write this function and you cannot look at its implementation)? Which value represents yes
and which no
? If you change the return type of that function to bool
, a type that only has 2 possible values: true
or false
, you immediately get what the return value means.
do I print that out in the main function
This is more of a design question and always depends on your very specific use case. Given only the code from the question: yes. You separated business logic, the part that does the prime check, from an IO / user interaction part. Generally, this makes code more readable and debuggable, two very important aspects of software development.