473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how can I return nothing?

I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?

Sep 19 '07 #1
80 40949
xi*****@gmail.com wrote On 09/19/07 13:06,:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
No. There's `return NULL;', but that's not
quite what you're after: even `NULL' is "something."

A C function either returns a value of a specified
type, or never returns a value of any kind (such a
function is written as if it "returned" a value of
the type `void').

There are a few avenues open to you. One is to
return either the prime number or some obvious non-
prime like 0 or -1; this is sometimes called "in-band
signaling." Another is to forget about returning the
prime value -- the caller already has a copy, after
all -- and just have the function test for primality
and return true or false. Still another is to have
the function return both the value and a primality
indicator, perhaps in a struct containing both, or
perhaps by "returning" a value via a pointer in the
argument list.

--
Er*********@sun.com
Sep 19 '07 #2
On Sep 19, 1:06 pm, xicl...@gmail.com wrote:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
Sure, you can return NULL in C - but that's not what you want here.
Your design thinking is flawed - how would you use the function if it
sometimes doesn't return a value? (It's barely legal not to return a
value from a function that is declared to return something, and it's
not at all legal to attempt to do something with the non-existent
return value.) What you probably want is a function that returns non-
zero if the number is a prime number and 0 if it is not. Using it
would be a snap:

#include <stdio.h>

int main( void ) {
if( your_prime_function(13) ) {
printf( "13 is prime.\n" );
}
if( !your_prime_function(14) ) {
printf( "14 is not prime.\n" );
}
return 0;
}

Sep 19 '07 #3
In article <11**********************@k35g2000prh.googlegroups .com>,
<xi*****@gmail.comwrote:
>I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
No, not in the sense you want. Your function returns an integer: you
can't return a non-integer from it. You can choose some "impossible"
integer value to use: 0 or -1 for example.

But why do you want it to return the number? It is more idiomatic in
C to just return true or false (non-zero or zero).

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 19 '07 #4
xi*****@gmail.com wrote:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?

If you mean a function that returns a value only when the input number is
prime, the answer is no. *

If you mean a function that returns one value when the input number is prime
and a DIFFERENT value when the number is NOT prime, the answer is yes. *
Sep 19 '07 #5
In article <11**********************@k35g2000prh.googlegroups .com>,
<xi*****@gmail.comwrote:
>I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
No, there isn't anything like that.

Suppose you had,
printf("Checking if %d is prime, got %d as the answer\n",
N, checkifprime(N));

If the number is prime then your hypothetical routine would return
the number and everything would be fine. But if the number were not
prime, and your hypothetical routine returned nothing, then what would
you expect to be the result of the above? If the routine returned
"nothing" then there would be no thing there to print out at the
second %d... what then??

You could set the routine up to return a pointer, with the pointer
being a pointer to the value if the number was prime, and with
the pointer being NULL if the number was not prime -- but the
NULL pointer is not "nothing", the null pointer is a "something"
whose value is a signal that there is nothing pointed to.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Sep 19 '07 #6
In article <11**********************@o80g2000hse.googlegroups .com>,
C. Benson Manica <cb******@gmail.comwrote:
>Your design thinking is flawed - how would you use the function if it
sometimes doesn't return a value? (It's barely legal not to return a
value from a function that is declared to return something, and it's
not at all legal to attempt to do something with the non-existent
return value.)
It's not necessarily true that his design is flawed, but it doesn't
fit well with the C way of doing things. In some Lisps, for example,
it's quite possible to have a function that returns a variable number
of values, including none.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 19 '07 #7
On Sep 19, 2:08 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>
It's not necessarily true that his design is flawed, but it doesn't
fit well with the C way of doing things. In some Lisps, for example,
it's quite possible to have a function that returns a variable number
of values, including none.
Hm, I wasn't aware of that. It sounds a bit odd, actually. Leave it
to Lithp... I appreciate the clarification.
Sep 19 '07 #8

<xi*****@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
Presumably your input to this function is a string.
You have several choices:

int isPrime( const char *in, unsigned long &value);

which returns 1 (true) if it is a prime, and 0 (false) if not,
and fills in the value.

Another approach:

unsigned long isPrime( const char *in );

where the return value is 0 if it is not a prime.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Sep 19 '07 #9
On Sep 19, 10:06 am, xicl...@gmail.com wrote:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
for:

unsigned long long primecheck(unsigned long long n);

Return 0 if composite and n if prime.

Then when you want to use it, do something like this:

if (n = primecheck(44177150871220967ULL)) printf("%llu is prime\n",
n);
else printf("%llu is composite\n", n);

Sep 19 '07 #10
On Wed, 19 Sep 2007 12:38:41 -0700, user923005 wrote:
Return 0 if composite and n if prime.

Then when you want to use it, do something like this:

if (n = primecheck(44177150871220967ULL)) printf("%llu is prime\n",
n);
else printf("%llu is composite\n", n);
This would display "0 is composite", it isn't what you meant, is
it?
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 19 '07 #11
On Wed, 19 Sep 2007 17:06:21 +0000, xicloid wrote:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
You can't in C. Either a function returns a value or it is a void
function. In the former case, returning nothing would cause UB if
the caller tries to use the result, and if it doesn't, how does it
know whether the number is prime? Even if you could <ot>(e.g. in
Python you can return None, or return with no expression)</ot>, I
can't think of any reason to do so.

What's wrong with returning 0?
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 19 '07 #12
In article <pa****************************@NOSPAM.it>,
Army1987 <ar******@NOSPAM.itwrote:
>This would display "0 is composite", it isn't what you meant, is
it?
But it is true.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 19 '07 #13
On Wed, 19 Sep 2007 18:34:07 +0000, C. Benson Manica wrote:
On Sep 19, 2:08 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>>
It's not necessarily true that his design is flawed, but it doesn't
fit well with the C way of doing things. In some Lisps, for example,
it's quite possible to have a function that returns a variable number
of values, including none.

Hm, I wasn't aware of that. It sounds a bit odd, actually. Leave it
to Lithp... I appreciate the clarification.
So you can in Python. Well, actually you can return a tuple or the
special built-in value None. The point is that an empty return
is considered false in boolean context. In C the obvious thing to
do is returning 0 when the number is composite.

--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 19 '07 #14
Army1987 wrote:
On Wed, 19 Sep 2007 18:34:07 +0000, C. Benson Manica wrote:
>On Sep 19, 2:08 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>>It's not necessarily true that his design is flawed, but it doesn't
fit well with the C way of doing things. In some Lisps, for example,
it's quite possible to have a function that returns a variable number
of values, including none.
Hm, I wasn't aware of that. It sounds a bit odd, actually. Leave it
to Lithp... I appreciate the clarification.

So you can in Python. Well, actually you can return a tuple or the
special built-in value None. The point is that an empty return
is considered false in boolean context. In C the obvious thing to
do is returning 0 when the number is composite.
You can in C too

int ReturnSomethingOrNothing(int arg,int *pResult)
{
int n = calc(arg);
if (n 100) {
*pResult = 100;
return 1;
}
return 0;
}

This returns whether there is a result and in that case the value
is stored in an int pointer, or zero, meaning there is no answer.
Sep 19 '07 #15
xi*****@gmail.com writes:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
I would change the function's interface so that it returns 1 if
the value is prime, 0 otherwise.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Sep 19 '07 #16
On Sep 19, 1:21 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <pan.2007.09.19.20.29.08.452...@NOSPAM.it>,

Army1987 <army1...@NOSPAM.itwrote:
This would display "0 is composite", it isn't what you meant, is
it?

But it is true.
Even though zero has an infinite number of factors it is not really a
composite number because of the definition of composite:
http://mathworld.wolfram.com/CompositeNumber.html

By definition, only Natural Numbers are allowed.

So Army1987 is right, there is a defect in the code.

A correct code would report "Neither prime nor composite" for all
numbers less than or equal to 1.
Sep 19 '07 #17

<xi*****@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
Robert Kaplan once wrote a whole book about nothing. It wasn't a joke or a
modern art type book. Nothing is a very interesting subject. For instance
the innovation of zero was condemned by the poet John Donne.
Is a string of no sausages the same thing as no string of sausages? You can
argue forever about that one.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 19 '07 #18
Op Wed, 19 Sep 2007 22:46:10 +0200 schreef jacob navia:
Army1987 wrote:
>On Wed, 19 Sep 2007 18:34:07 +0000, C. Benson Manica wrote:
>>On Sep 19, 2:08 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
It's not necessarily true that his design is flawed, but it doesn't
fit well with the C way of doing things. In some Lisps, for example,
it's quite possible to have a function that returns a variable number
of values, including none.
Hm, I wasn't aware of that. It sounds a bit odd, actually. Leave it
to Lithp... I appreciate the clarification.

So you can in Python. Well, actually you can return a tuple or the
special built-in value None. The point is that an empty return
is considered false in boolean context. In C the obvious thing to
do is returning 0 when the number is composite.

You can in C too

int ReturnSomethingOrNothing(int arg,int *pResult)
^^^
{
int n = calc(arg);
if (n 100) {
*pResult = 100;
return 1;
}
return 0;
}

This returns whether there is a result and in that case the value
is stored in an int pointer, or zero, meaning there is no answer.
The return type is int, so it returns an int.
Zero in this case is an answer ;-(
--
Coos
Sep 19 '07 #19
Coos Haak wrote:
Op Wed, 19 Sep 2007 22:46:10 +0200 schreef jacob navia:
>Army1987 wrote:
>>On Wed, 19 Sep 2007 18:34:07 +0000, C. Benson Manica wrote:

On Sep 19, 2:08 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
It's not necessarily true that his design is flawed, but it doesn't
fit well with the C way of doing things. In some Lisps, for example,
it's quite possible to have a function that returns a variable number
of values, including none.
Hm, I wasn't aware of that. It sounds a bit odd, actually. Leave it
to Lithp... I appreciate the clarification.
So you can in Python. Well, actually you can return a tuple or the
special built-in value None. The point is that an empty return
is considered false in boolean context. In C the obvious thing to
do is returning 0 when the number is composite.
You can in C too

int ReturnSomethingOrNothing(int arg,int *pResult)
^^^
>{
int n = calc(arg);
if (n 100) {
*pResult = 100;
return 1;
}
return 0;
}

This returns whether there is a result and in that case the value
is stored in an int pointer, or zero, meaning there is no answer.

The return type is int, so it returns an int.
Zero in this case is an answer ;-(
1 for "There is an answer and pointer containes its value"
0 for "There is no answer"
Sep 19 '07 #20
"Fred Kleinschmidt" <fr******************@boeing.comwrites:
<xi*****@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...
>I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?

Presumably your input to this function is a string.
[...]

Why do you presume that? The OP specifically referred to the "input
integer".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 19 '07 #21
"Malcolm McLean" <re*******@btinternet.comwrites:
[...]
Robert Kaplan once wrote a whole book about nothing. It wasn't a joke
or a modern art type book. Nothing is a very interesting subject. For
instance the innovation of zero was condemned by the poet John Donne.
Is a string of no sausages the same thing as no string of sausages?
You can argue forever about that one.
<OT>
It's on my bookshelf, preceded by Eli Maor's book about e, Paul
J. Jahin's book about i (sqrt(-1)), petr beckmann's book about pi, and
the Beatles' album "1".
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 19 '07 #22
In article <pa****************************@NOSPAM.it>,
Army1987 <ar******@NOSPAM.itwrote:
>>It's not necessarily true that his design is flawed, but it doesn't
fit well with the C way of doing things. In some Lisps, for example,
it's quite possible to have a function that returns a variable number
of values, including none.
>Hm, I wasn't aware of that. It sounds a bit odd, actually. Leave it
to Lithp... I appreciate the clarification.
>So you can in Python. Well, actually you can return a tuple or the
special built-in value None.
Lisp of course has always had the option of returning a list or nil
(which is the same as the empty list in most Lisps), but that's not
what I was referring to.

Common Lisp has a mechanism specifically for returning multiple
values. If a function uses (values 1 2 3) to return, it returns three
values, 1, 2, and 3. A caller that does not care about this just sees
the single value 1, but a caller that wants all the values can see
them using constructs such as multiple-value-bind. If a function uses
(values) to return, it returns no values. A caller that doesn't care
about this will instead see the value nil returned.

For example, the floor function returns two values: the rounded-down
value of its argument and the remainder. Callers that only want the
rounded-down value (probably the usual case) just treat it as an
ordinary function, and don't see the second return value.
Sufficiently Clever [tm] Compilers can detect this and avoid
calculating the unnecessary value.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 19 '07 #23
On Wed, 19 Sep 2007 23:17:15 +0200, Coos Haak wrote:
Op Wed, 19 Sep 2007 22:46:10 +0200 schreef jacob navia:
>This returns whether there is a result and in that case the value
is stored in an int pointer, or zero, meaning there is no answer.
The return type is int, so it returns an int.
Zero in this case is an answer ;-(
Which is *exactly* what I suggested in the post he was replying
to. He's still in my killfile, but I see he isn't showing any sign
of getting even ever-so-slightly closer to posting anything
useful...
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 19 '07 #24
On 19 sep, 17:06, xicl...@gmail.com wrote:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
well you have one issue.in stead of returnig nothing,the fonction will
return a value like 0 or -1.
then in the main programme you can add a test so you can be sure if
it's a prime number or not.

#incldue<stdio.h>
int n;
int prime(int x)
{int r;
if(x%2!=0) r=x;
else r=0;
return r;
}
void main()
{
printf("give your number");scanf("%d",&n);
if(prim(n)!=0)
printf("%d",n);
}
getch();

7aT!M

Sep 19 '07 #25
"Malcolm McLean" <re*******@btinternet.comwrites:
>Robert Kaplan once wrote a whole book about nothing. It wasn't a joke
or a modern art type book. Nothing is a very interesting subject. For
instance the innovation of zero was condemned by the poet John Donne.
Is a string of no sausages the same thing as no string of sausages?
You can argue forever about that one.
Keith Thompson <ks***@mib.orgwrites:
It's on my bookshelf, preceded by Eli Maor's book about e, Paul
J. Jahin's book about i (sqrt(-1)), petr beckmann's book about pi, and
the Beatles' album "1".
Well... i is not really sqrt(-1) since there are two complex numbers
which are not equal to one another which power of two is equal -1 and
i cannot be equal to both numbers. :P

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 19 '07 #26
In article <87************@erwin.mina86.com>,
Michal Nazarewicz <mi****@tlen.plwrote:
>Well... i is not really sqrt(-1) since there are two complex numbers
which are not equal to one another which power of two is equal -1 and
i cannot be equal to both numbers. :P
Would you also say that 2 is not really sqrt(4), since there are
two real numbers whose square is 4 and 2 cannot be equal to both
of them?

(You do have a point though: there is no way to distinguish between
i and -i.)

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 19 '07 #27
On Sep 19, 11:13 am, Eric Sosman <Eric.Sos...@sun.comwrote:
xicl...@gmail.com wrote On 09/19/07 13:06,:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?

No. There's `return NULL;', but that's not
quite what you're after: even `NULL' is "something."

A C function either returns a value of a specified
type, or never returns a value of any kind (such a
function is written as if it "returned" a value of
the type `void').

There are a few avenues open to you. One is to
return either the prime number or some obvious non-
prime like 0 or -1; this is sometimes called "in-band
signaling." Another is to forget about returning the
prime value -- the caller already has a copy, after
all -- and just have the function test for primality
and return true or false. Still another is to have
the function return both the value and a primality
indicator, perhaps in a struct containing both, or
perhaps by "returning" a value via a pointer in the
argument list.

--
Eric.Sos...@sun.com
Another (perverse) alternative is to not return at all... in a
conventional way. Use longjump() for the "emergency return" from the
function.

Alex

Sep 20 '07 #28
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <87************@erwin.mina86.com>,
Michal Nazarewicz <mi****@tlen.plwrote:
>>Well... i is not really sqrt(-1) since there are two complex numbers
which are not equal to one another which power of two is equal -1 and
i cannot be equal to both numbers. :P

Would you also say that 2 is not really sqrt(4), since there are
two real numbers whose square is 4 and 2 cannot be equal to both
of them?

(You do have a point though: there is no way to distinguish between
i and -i.)
Um, -i is the one with the minus sign in front of it.

Seriously, I'm not sure what you mean by "no way to distinguish". For
positive operands, only the positive square root is considered to be
*the* square root (e.g., sqrt(4.0) is 2.0, not -2.0). Similar rules
apply for negative and even complex operands.

C99 supports a complex square root function, csqrt (plus csqrtf and
csqrtl for float complex and long double complex, respectively).
C99 7.3.8.3 says:

The csqrt functions compute the complex square root of z, with a
branch cut along the negative real axis.

The csqrt functions return the complex square root value, in the
range of the right halfplane (including the imaginary axis).

Both in C and in mathematics, sqrt(-1) is i, not -i (or I as C's
<complex.hcalls it).

(See, we're topical again!.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 20 '07 #29
On Sep 19, 6:53 pm, "7atim...@gmail.com" <7atim...@gmail.comwrote:
well you have one issue.in stead of returnig nothing,the fonction will
return a value like 0 or -1.
That's a bit fractured, but at least it's feasible. The code you
posted, however...
#incldue<stdio.h>
int n;
int prime(int x)
{int r;
if(x%2!=0) r=x;
else r=0;
return r;}

void main()
{
printf("give your number");scanf("%d",&n);
if(prim(n)!=0)
printf("%d",n);}

getch();
....contains two obvious typos, an invalid declaration for main, and a
misplaced invocation of a nonstandard function. (It also has some
problems at runtime, assuming it were fixed to a point where it could
be compiled.)

Sep 20 '07 #30
On Wed, 19 Sep 2007 13:56:35 -0700, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
>xi*****@gmail.com writes:
>I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.

I would change the function's interface so that it returns 1 if
the value is prime, 0 otherwise.
Good advice. And name the function something like "is_prime".

/*prototype*/
int is_prime(int i);

/* code that calls is_prime()*/
if ( is_prime(i) )
{
/*i is a prime number*/
}

Regards
--
jay
Sep 20 '07 #31
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>(You do have a point though: there is no way to distinguish between
i and -i.)
>Um, -i is the one with the minus sign in front of it.
:-)
>Seriously, I'm not sure what you mean by "no way to distinguish". For
positive operands, only the positive square root is considered to be
*the* square root (e.g., sqrt(4.0) is 2.0, not -2.0). Similar rules
apply for negative and even complex operands.
Right, but which square root of -1 is the positive one?

Imagine you're talking to an alien, and you want to describe the
square roots of 4. There are two of them, and one of them happens to
have the property that when you add it to itself, you get four again.
So you can easily specify which one you're talking about (and of
course you can do it in lots of other ways).

Now you want to describe the square roots of -1. There are two of
them, but which is the one you call i? Suppose he calls the square
root of -1 j: is j the same as i or is it -i? There's no way to tell.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 20 '07 #32
xi*****@gmail.com wrote:
I'm making a function that checks the input integer and
returns the value if it is a prime number.
If the integer is not a prime number, then the function should
return nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?
Not exactly, but if the interface really cannot be changed,
here you are:

int checkprime (int i)
{
int isprime = 1;
/* code to set isprime to 0 if i is not prime */
if (isprime)
return i;
}

Of course the caller of this function must take special care.
The following would not yet cross the frontier to the land
of undefined behavior:

#include <stdio.h>
int main (void)
{
if (checkprime(32323))
puts("32323 is prime");
checkprime(31313);
puts("31313 is not prime");
return 0;
}

Ralf
Sep 20 '07 #33
"C. Benson Manica" <cb******@gmail.comwrote:
On Sep 19, 2:08 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:

It's not necessarily true that his design is flawed, but it doesn't
fit well with the C way of doing things. In some Lisps, for example,
it's quite possible to have a function that returns a variable number
of values, including none.

Hm, I wasn't aware of that. It sounds a bit odd, actually. Leave it
to Lithp... I appreciate the clarification.
It's a perfectly good and perfectly normal way to handle such functions
in dynamically typed languages. You could do something like

list_print(object)
if typeof object = string
print "'" trim(object) "'"
elseif typeof object = number
print format(object, "*.##", false)
elseif typeof object = logical
print (if object: "yes" else: "no")
elseif typeof object = null
print "(nothing)"
else
print "(something odd)"
end

C, however, is statically typed, so this doesn't work, at least not
without using a struct or something similar.

Richard
Sep 20 '07 #34
Op 20 Sep 2007 09:57:05 GMT schreef Richard Tobin:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>(You do have a point though: there is no way to distinguish between
i and -i.)
>>Um, -i is the one with the minus sign in front of it.

:-)
>>Seriously, I'm not sure what you mean by "no way to distinguish". For
positive operands, only the positive square root is considered to be
*the* square root (e.g., sqrt(4.0) is 2.0, not -2.0). Similar rules
apply for negative and even complex operands.

Right, but which square root of -1 is the positive one?

Imagine you're talking to an alien, and you want to describe the
square roots of 4. There are two of them, and one of them happens to
have the property that when you add it to itself, you get four again.
So you can easily specify which one you're talking about (and of
course you can do it in lots of other ways).

Now you want to describe the square roots of -1. There are two of
them, but which is the one you call i? Suppose he calls the square
root of -1 j: is j the same as i or is it -i? There's no way to tell.
i nor -i is the square root of -1.
i squared and -i squared both equal -1.
Not the other way round.
--
Coos
Sep 20 '07 #35
In article <1x****************************@40tude.net>,
Coos Haak <ch*****@hccnet.nlwrote:
>i nor -i is the square root of -1.
What makes you think that?

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 20 '07 #36
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <1x****************************@40tude.net>,
Coos Haak <ch*****@hccnet.nlwrote:
>>i nor -i is the square root of -1.

What makes you think that?
The same thing I've pointed. In real number domain a square root of
x is defined to be _nonnegative_ number which squared gives x. You
cannot apply analogical definitions to complex numbers since there is no
such think as nonnegative complex number (or nonpositive for that
matter).

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 20 '07 #37
Michal Nazarewicz said:
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
>In article <1x****************************@40tude.net>,
Coos Haak <ch*****@hccnet.nlwrote:
>>>i nor -i is the square root of -1.

What makes you think that?

The same thing I've pointed. In real number domain a square root of
x is defined to be _nonnegative_ number which squared gives x. You
cannot apply analogical definitions to complex numbers since there is no
such think as nonnegative complex number (or nonpositive for that
matter).
Sorry, but I must disagree. I can accept that there is no such thing as a
positive imaginary number, and no such thing as a negative imaginary
number, but I cannot accept that there is no such thing as a non-negative
*complex* number. A complex number has a (possibly zero but usually not)
real number element which is sufficient to displace it off the zero line
in the complex plane. Most numbers in the complex plane are either to the
right of the zero line (positive) or to its left (negative), regardless of
the value of their imaginary co-ordinate.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 20 '07 #38
In article <87************@erwin.mina86.com>,
Michal Nazarewicz <mi****@tlen.plwrote:
>>>i nor -i is the square root of -1.
>What makes you think that?
>The same thing I've pointed. In real number domain a square root of
x is defined to be _nonnegative_ number which squared gives x.
No, this is not true. The sqrt() function is defined to return the
non-negative square root, and the square root symbol has the same
convention, but a number (other than zero) has two square roots. Of
course you might be able to find someone who uses a different
convention, but it's not usual. Search for square root with Google,
and look at the first few references such as Wolfram and Wikipedia.

The same applies to other uses of "root" in maths: consider nth roots
of unity, roots of equations, and so on.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 20 '07 #39
Michal Nazarewicz wrote, On 20/09/07 17:03:
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
>In article <1x****************************@40tude.net>,
Coos Haak <ch*****@hccnet.nlwrote:
>>i nor -i is the square root of -1.
What makes you think that?

The same thing I've pointed. In real number domain a square root of
x is defined to be _nonnegative_ number which squared gives x. You
cannot apply analogical definitions to complex numbers since there is no
such think as nonnegative complex number (or nonpositive for that
matter).
Actually you can apply an suitable definition such as the one included
in the latest draft of the C standard. In
http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf section
7.3.8.3 it specified "The csqrt functions return the complex square root
value, in the range of the right half-plane (including the imaginary axis)."

I'm not sure this is entirely fully specified, I think it should be
"including the the origin and the top half of the imaginary axis" rather
than simply "including the imaginary axis" so as to select i and exclude -i.

I'm adding comp.std.c to the group list since I think this is a defect
in the draft.

If you just want to discus imaginary numbers and the maths I suggest
taking it to a maths group, if you want a ruling on whether it is a
defect in the standard (as I do, as a matter of interest rather than
because I use it) then comp.std.c is appropriate IMHO.
--
Flash Gordon
Sep 20 '07 #40
Ralf Damaschke <rw****@gmx.dewrites:
xi*****@gmail.com wrote:
>I'm making a function that checks the input integer and
returns the value if it is a prime number.
If the integer is not a prime number, then the function should
return nothing.
Problem is, I don't know how to do that.
Isn't there anything like "return null"in C?

Not exactly, but if the interface really cannot be changed,
here you are:

int checkprime (int i)
{
int isprime = 1;
/* code to set isprime to 0 if i is not prime */
if (isprime)
return i;
}

Of course the caller of this function must take special care.
The following would not yet cross the frontier to the land
of undefined behavior:

#include <stdio.h>
int main (void)
{
if (checkprime(32323))
puts("32323 is prime");
checkprime(31313);
puts("31313 is not prime");
return 0;
}
What a magnificantly *bad* idea (as I'm sure it was intended to be)!
You can't safely use the result of a call to checkprime() unless you
already know whether the argument you passed it is prime.

Sorry if I'm spoiling the joke by explaining it, but I wanted to make
sure the OP understands what's going on.

(It *was* a joke, right?)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 20 '07 #41
Michal Nazarewicz <mi****@tlen.plwrites:
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
>In article <1x****************************@40tude.net>,
Coos Haak <ch*****@hccnet.nlwrote:
>>>i nor -i is the square root of -1.

What makes you think that?

The same thing I've pointed. In real number domain a square root of
x is defined to be _nonnegative_ number which squared gives x. You
cannot apply analogical definitions to complex numbers since there is no
such think as nonnegative complex number (or nonpositive for that
matter).
If the square root function is defined simply as the number (or a
number) whose square is the argument, then every non-zero number,
real, imaginary, or complex, has two possible square roots.

By convention, for positive real values, the positive value is
considered to be the principle square root (I think that's the right
terminology); sqrt(4) == 2, sqrt(4) != -2.

I *think* there's also a mathematical convention for choosing the
principle square root for imaginary and complex operands. I'm not
certain of that, and the convention could vary among different
branches of mathematics, or even among different mathematicians. But
C defines a csqrt() function that yields the complex square root of a
complex operand, and that function definitely defines a convention for
choosing one value when there are two possibilities. C99 7.3.8.3:

The csqrt functions compute the complex square root of z, with a
branch cut along the negative real axis.

The csqrt functions return the complex square root value, in the
range of the right halfplane (including the imaginary axis).

I *think* that's consistent with conventional mathematical usage; that
certainly should be the intent.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 20 '07 #42
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>(You do have a point though: there is no way to distinguish between
i and -i.)
>>Um, -i is the one with the minus sign in front of it.

:-)
>>Seriously, I'm not sure what you mean by "no way to distinguish". For
positive operands, only the positive square root is considered to be
*the* square root (e.g., sqrt(4.0) is 2.0, not -2.0). Similar rules
apply for negative and even complex operands.

Right, but which square root of -1 is the positive one?

Imagine you're talking to an alien, and you want to describe the
square roots of 4. There are two of them, and one of them happens to
have the property that when you add it to itself, you get four again.
So you can easily specify which one you're talking about (and of
course you can do it in lots of other ways).

Now you want to describe the square roots of -1. There are two of
them, but which is the one you call i? Suppose he calls the square
root of -1 j: is j the same as i or is it -i? There's no way to tell.
So you're saying that if you took the complex plane (the set of all
complex numbers) and *relabelled* every number with a non-zero
imaginary part, swapping '+' for '-' on the imaginary part of each
number, then all operations would continue to yield exactly the same
results (except for the somewhat arbitrary choice of which value to
consider as the principle value for potentially multi-valued functions
like sqrt).

I hadn't thought about it before, but it certainly seems plausible.

The answer is that you *arbitrarily* pick one half-plane as "positive"
and the other as "negative" -- and the minus sign really is how you
tell the difference beteen i and -i.

Visualizing the set of complex numbers as a Cartesian plane, or even
the set of real numbers as a line, is equally arbitrary. It's useful
because it corresponds well to the way the human mind works, but
there's nothing mathematically necessary about it.

To return to some semblance of topicality, many of C99's rules for
complex numbers are based on arbitrary mathematical conventions, not
necesarily on fundamental mathematical truths. But it works.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 20 '07 #43
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
>Right, but which square root of -1 is the positive one?

Imagine you're talking to an alien, and you want to describe the
square roots of 4. There are two of them, and one of them happens to
have the property that when you add it to itself, you get four again.
So you can easily specify which one you're talking about (and of
course you can do it in lots of other ways).

Now you want to describe the square roots of -1. There are two of
them, but which is the one you call i? Suppose he calls the square
root of -1 j: is j the same as i or is it -i? There's no way to tell.
Keith Thompson <ks***@mib.orgwrites:
So you're saying that if you took the complex plane (the set of all
complex numbers) and *relabelled* every number with a non-zero
imaginary part, swapping '+' for '-' on the imaginary part of each
number, then all operations would continue to yield exactly the same
results.
Of course. Funny thing that it also works for real numbers. :) But in
real numbers terms such as "positive" and "negative" are well defined,
which is not the case for complex numbers. It's the same with relations
-- we cannot say that complex number w is greater then complex number
z because this relation is not defined for complex numbers. (Come to
think of it we use this relation to define positive and negative
numbers.)
I hadn't thought about it before, but it certainly seems plausible.

The answer is that you *arbitrarily* pick one half-plane as "positive"
and the other as "negative" -- and the minus sign really is how you
tell the difference beteen i and -i.
And numbers on a line which divides the plane into to half-planes are
all zeros. O_o I assume it's not as easy as one may expect. Also there
are unexpected results, like 1-i being positive (negative) or i-1 being
negative (positive).
Visualizing the set of complex numbers as a Cartesian plane, or even
the set of real numbers as a line, is equally arbitrary. It's useful
because it corresponds well to the way the human mind works, but
there's nothing mathematically necessary about it.
Probably yes.
To return to some semblance of topicality, many of C99's rules for
complex numbers are based on arbitrary mathematical conventions, not
necesarily on fundamental mathematical truths. But it works.
Well... All "fundamental mathematical truths" are some kind of
"arbitrary mathematical conventions" -- they are called axioms.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 20 '07 #44
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>(It *was* a joke, right?)
I certainly hope so.

Here's another:

int checkprime (int i)
{
int isprime = 1;
/* code to set isprime to 0 if i is not prime */
if (isprime)
return i;
else
while(1)
;
}

It certainly doesn't return anything if the number is not prime.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 20 '07 #45
In article <87************@erwin.mina86.com>,
Michal Nazarewicz <mi****@tlen.plwrote:
>So you're saying that if you took the complex plane (the set of all
complex numbers) and *relabelled* every number with a non-zero
imaginary part, swapping '+' for '-' on the imaginary part of each
number, then all operations would continue to yield exactly the same
results.
>Of course. Funny thing that it also works for real numbers. :)
No it doesn't, if I understand you correctly. If you take

1*1 = 1

and relabel 1 as -1, it becomes false.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 20 '07 #46
Flash Gordon wrote:
http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf section
7.3.8.3 it specified "The csqrt functions return the complex square root
value, in the range of the right half-plane (including the imaginary axis)."
I'm not sure this is entirely fully specified, I think it should be
"including the the origin and the top half of the imaginary axis" rather
than simply "including the imaginary axis" so as to select i and exclude -i.
I'm adding comp.std.c to the group list since I think this is a defect
in the draft.
No, see section 7.3.3 paragraph 1. The idea is to allow the sign of
the 0-valued imaginary part to select which half of the axis is reported.
(IEEE floating-point supports distinct +0 and -0.)
Sep 20 '07 #47
In article <46***************@null.net>,
Douglas A. Gwyn <DA****@null.netwrote:
>No, see section 7.3.3 paragraph 1. The idea is to allow the sign of
the 0-valued imaginary part to select which half of the axis is reported.
(IEEE floating-point supports distinct +0 and -0.)
So should sqrt(-1 + (-0.0)*I) return -I?

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 20 '07 #48
On Sep 20, 3:02 pm, Keith Thompson <ks...@mib.orgwrote:
Both in C and in mathematics, sqrt(-1) is i, not -i (or I as C's
<complex.hcalls it).
Mathematics does not have "sqrt" . The square root
function is a "multi-valued function" (a slight
oxymoron, but it is the usual term). The value of
sqrt(9) is the set {3, -3}. The positive term is
known as the "principal value" of the multi-valued
function. Alternatively, the "principal square root"
function is the (single-valued) function that
consists of the principal value of the square root.

The C99 csqrt uses the grotty concept of a
'branch cut' to always select one particular one
of the two values of the square root. It's arbitrary
as to where you place the cut; e.g. it could equally
well have been defined that sqrt(-9) == -3*I .
Sep 20 '07 #49
In article <11**********************@e34g2000pro.googlegroups .com>,
Old Wolf <ol*****@inspire.net.nzwrote:
>Mathematics does not have "sqrt" . The square root
function is a "multi-valued function" (a slight
oxymoron, but it is the usual term).
"sqrt" is a textual rendition of the operator symbol written:

________
_ /
\/

That operator is usually taken to refer to the principal square root.
(Consider how the quadratic formula is usually written, with +/-
before the operator, which would be unnecessary if it meant both
values.)

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 20 '07 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
5
by: Edward Diener | last post by:
I am gathering from the documentation that return values from __events are not illegal but are frowned upon in .NET. If this is the case, does one pass back values from an event handler via...
4
by: WebBuilder451 | last post by:
I have a function that returns a dataset or a boolean depending upon whether a record was found. I can check for the string value of the return type, but i don'r think this is the right way to do...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
8
by: gregory_may | last post by:
Is it possible to return "nothing" from an Integer function? This seems to give me "0" rather than "nothing". Private Function MyFunction() As Integer Return Nothing End Function
11
by: Joe HM | last post by:
Hello - I have the following very simple code ... Dim lStringA As String = "" Dim lStringB As String = "" .... lStringB = Replace(lStringA, "DUMMY", "", Compare:=CompareMethod.Text)
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
9
by: Doug | last post by:
Hi I have a method that I would like to return the value to the calling event. I have a bad example below that doesnt give me what I want. What I want is to have a my readxml mthod return the...
10
by: Atara | last post by:
Suppose I have the following functions: Public Function myF1ToStream(...) As System.IO.MemoryStream . . . Dim ms As New System.IO.MemoryStream(buf) Return ms End Function Public Function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.