|
-
February 2nd, 2011, 03:46 PM
#1
Help with prime number program
Beginner here, just want to clarify something.
In an int/boolean function, regardless of where the first return value originates (e.g. from a loop), does the first value returned terminate the function (and the loop it is in, if it is in a loop) immediately?
So an example, a program which tells you if a number is a prime number.
Code:
/* FUNCTION TO EVALUATE IF THE ARGUMENT IS A PRIME BETWEEN 1 AND 1000 */
enum Logical {False, True};
Logical small_prime(int integer)
{ for (int factor = 2; factor<integer; factor++)
{
if ( (integer % factor) == 0)
return False;
}
return True;
}
Originally I was confused as I thought this function would return True no matter what happened in the for loop. The return true statement comes after it so I assumed after the loop finished it would read that (change false to true) and act accordingly. Now I realise this function only makes sense if the above deduction is true.
Thanks
KingG
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|