The purpose of this lab is to explore functions
- Create a New Project
- We want to create a program that uses a function to
calculate the first n primes, where the user gets to enter
n.
We begin by writing a main program that will simply call a function
named prime.
This code
will not run; We need to "declare" the function and write
the code associated with the function. This
code
doesn't do anything, yet. But examine its "form";
- Prior to main the prime function is declared. It has a type prior
to its name and a type inside the parentheses.
It ends with a ;
- After main, the prime function is defined. Again there is a type prior
to the name. This one has a declaration statement inside the parentheses.
- The code in the function looks like main.
- There is a return
This function is called a stub. It does nothing, just reports what
is supposed to be done.
- Now, let's fill in the details of the prime function.
This code does the remainder check and raises
a flag if the number is NOT prime.
- Now, we need to repeat the code to find a series of primes. This
code
checks the first 10 numbers to see if they're prime.
The couts are a bit annoying, but when the code is verified, we can remove
then.
- Now, lets request the user to enter the number of primes that they would
like to find. This
code
asks the user for the number of primes, but doesn't produce as expected.
- This code
is corrected by fixing the comparison in the loop.