how can I return nothing?

K

Keith Thompson

Fred Kleinschmidt said:
Presumably your input to this function is a string.
[...]

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

Keith Thompson

Malcolm McLean said:
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>
 
R

Richard Tobin

Hm, I wasn't aware of that. It sounds a bit odd, actually. Leave it
to Lithp... I appreciate the clarification.
[/QUOTE]
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
 
M

Malcolm McLean

Keith Thompson said:
<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>
One would have thought that the Beatles would write an album on the roots of
unity.
Anyway, there's an obvious gap in the market for a title, phi.
 
A

Army1987

Op Wed, 19 Sep 2007 22:46:10 +0200 schreef jacob navia:
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...
 
7

7atim.Ld

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
 
R

Richard Tobin

Michal Nazarewicz said:
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
 
M

Michal Nazarewicz

Keith Thompson said:
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
 
A

Alexei A. Frounze

(e-mail address removed) wrote On 09/19/07 13:06,:


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.

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

Alex
 
K

Keith Thompson

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.h> calls it).

(See, we're topical again!.)
 
C

C. Benson Manica

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.)
 
J

jaysome

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
 
C

Coos Haak

Op 20 Sep 2007 09:57:05 GMT schreef Richard Tobin:
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.
[/QUOTE]
i nor -i is the square root of -1.
i squared and -i squared both equal -1.
Not the other way round.
 
R

Richard Tobin

(You do have a point though: there is no way to distinguish between
i and -i.)
[/QUOTE]
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
 
R

Ralf Damaschke

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
 
R

Richard Bos

C. Benson Manica said:
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
 
M

Michal Nazarewicz

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).
 
R

Richard Heathfield

Michal Nazarewicz said:
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.
 
R

Richard Tobin

What makes you think that?
[/QUOTE]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top