how do I wrtie a function that takes one argument of type double. The function returns the character value ‘P’ if its argument is positive and return ‘N’ if its argument is zero or negative.

double Letter ( double P, double N)

if (P = +);

return P;

return N

if ( P=0);

Does this look right?

9 Years
Discussion Span
Last Post by kjc367

>>Does this look right?
Close, but its not right

1) line 1: the function is supposed to return a char, not double. So you need to declare it as char Letter ( double P) 2) The function is only supposed to have one argument, not two.

3) You forgot the open and close braces around the body of the function.

4) line 3: remove the semicolon at the end of that line.

5) The return values on lines 5 and 9 are wrong. You are supposed to return the letter P is the parameter is a positive value. That means you have to compare the value of the parameter to 0. If greater than or equal to 0 then return 'P' otherwise return 'N'.

6) Delete line 11 because I have no idea what that is supposed to do.

char Letter ( double P, double N)

if( P >= 0)

return P

else return N


How about this AD?


that's better but still not correct. You need to put single quotes around the P and N, like this: return 'P'; Also see my comment #2 in my previous post -- you didn't remove that second parameter.

Next you probably need to add a main() function so that your program will compile and link correctly.


so how do I return 'N' if I don't delcare it?

There is no need to initialise and declare the return value, in this case. It's returned directly without first assigning it to a variable. If you wanted to, you could initialise and assign to a char variable and then return that char variable - that is the same as return 'P'; .

Your function is designed to return a char value, as long as you return a char value everything is sweet. As AD mentioned, you don't need to pass a char variable as an argument, it serves no purpose.

int main()
   double P = 15.6;
   char z;
   z = Letter(P);

In the above example z will be assigned the return value (char) of the Letter(P) function.


Consider the following:

inline char IsPositive(double r) { return ( (r < 0.0) ? 'N' : 'P'); }
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.