I think I have a hard time understanding the return() statement.
I have to make a program where a user gives the lower limit and upper limit as input.
After that, the program should give all prime numbers that are between those 2 numbers.

A testcase would be:

input:
10
30
output:
A list of all prime numbers between the lower and upper limit.
11
13
17
19
23
29

The part that actually calculates if a number is a prime number has been put into a function is_prime.

What i want the program to do is to return a 1 or 0 based on the value of is_prime which depends on if the function is_prime has found a prime number or not.

``````#include <stdio.h>

int number = 0;
int result = 1;

int is_prime(result)
{
/*Declare variables*/

int testcases = 4;
int N;
int i = 0;

/* implement 4 testcases */
for(N = 0; N < testcases; N++)
{
printf("\n\nVoer een getal in: ");
scanf("%d", &number);

/*Numbers smaller than 2 aren't prime numbers*/
if (number < 2)
{
printf("%d is geen priemgetal", number);
result = 0;
}

else
{
i = 2;
while ((i*i) <= number)
{
/*Checks if number divided by i is an even number*/
if ((number % i) == 0)
{
result = 0;
break;
}
else
{
i += 1;
}
}
/*Determine if number is a prime number based on the value of result*/
if (result == 0)
{
printf("%d is geen priemgetal", number);
}
if (result == 1)
{
printf ("%d is een priemgetal", number);
}
}
}
return result;
}

int main(void)

{
int upper_limit = 0;
int i = 0;
int lower_limit = i;

printf("Upper limit: ");
scanf("%d", &upper_limit);
printf(" lower limit: ");
scanf("%d", &lower_limit);

while (i < upper_limit)
if (is_prime(result))
{
printf("%d", number);
}
else
{
i += 1;
}

return 0;
}``````

This is the nassi-schneiderman diagram that goes with it.
http://img836.imageshack.us/f/primes.gif/

Because you have it set to return Boolean values (0 or 1), you can use a construct like this:

``````if(is_prime(number))
printf("\n%d is prime", number);
else
printf("\n%d is not a prime number", number);

if(is_prime(number))
printf("\n%d is prime", number);
else
printf("\n%d is not a prime number", number);

The return from a function is normally "caught" by a variable, like so:
myVariable = is_prime(number);

if(myVariable == 1)
etc.
else
etc.``````

If you don't "catch" the returning value (and always do so with the right data type, of course), then the return value is lost. (when is determined by the OS)

The "golden rule" with returned values is:
"Don't ever return the address (a pointer), to a local variable"!

Since the local variable is marked as no longer valid, the address may or may not, be valid, and leads to bugs that are VERY hard to sort out, later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.