Okay so I'm completely new to functions and this was an example code provided by our instructor. To be honest I haven't learned much from it and feel totally lost about functions. Don't mean to be a pain...but could somebody please go line by line and tell me what's happening and how to correctly write a function?
//CSC150 Lab1
//Ian Heinze
//10/15/2015
#include <iostream>
usingnamespace std;
int main()
{
char answer = 'y';
int prime;
bool isPrime(int prime);
while (answer == 'y' || answer == 'Y')
{
cout << "Please enter a number to determine if it is prime or not: " << endl;
cin >> prime;
if (prime <= 0)
{
cout << "Please check your number. (Cannot be below 0)." << endl;
cin >> prime;
return 0;
}
if (isPrime(prime))
cout << prime << " is a prime number. " << endl;
else
cout << prime << " is not a prime number. " << endl;
cout << "If you would like to try another number enter (Y/y) if not, (N/n)" << endl;
cin >> answer;
}
return 0;
}
bool isPrime(int a)
{
for ( int i = 2; i <= a/2; i++)
{
if (a % i ==0)
return 0;
}
returntrue;
}