Functions

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  //CSC150 Lab1
//Ian Heinze
//10/15/2015

#include <iostream>


using namespace 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;
}

return true;

}
Topic archived. No new replies allowed.