Function not returning a correct value 
Author Message
 Function not returning a correct value

Hi all,
I'm trying to get Function 2 to work, but it doesn't. I got Function 1 to
work just fine. Whats wrong with Function 2? I haven't got to Function 3
yet....

Thanks,
Mike

/***************************************************************************
                          assignment_5_problem_1.c  -  description
                             -------------------
    Student              : Michael
    Class                : CIS 150 D
    Date                 : Wed Oct 9 2002

Instructor:

This program will calculate the average rainfall for a year using the
average rainfall reported for four quarters. The program will need to ask
the user to enter an average rainfall for each quarter, add the amounts
entered together and divide them by 4. The result should be output to the
user. For this assignment you will need:

    * A function that gets one average rainfall value. This function will
use error checking so that no values less than 1 or greater than 100 will
be accepted. The function will need no parameters and will return a float.

    * A function that will add and average the 4 numbers entered into the
program. This function will take 4 floats as parameters and will return a
float.

    * A function that will output the results. This function will display
the four quarterly values and the average. It will return nothing.

****************************************************************************/
# include <stdio.h>
# define SIZE 4

float avgRainInput ( float );
float annualAvg ( float, float, float, float );
void endOfYearTotals ( float, float, float, float, float );

int main ()
{

    float rain[ SIZE ];
    int q = 1;
    float k;

    printf ( "Rainfall Calculator\n");
    while ( q <= SIZE ) {
    printf ( "Enter rainfall value for Quarter %d\n", q );
    scanf ( "%f", &k );
    avgRainInput( k );
    rain[ q - 1 ] = k;
    q++;
    }
    annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );
    printf ( "annualAvg = %f\n", annualAvg );

Quote:
}

float avgRainInput ( float j ) /* Function 1: Get one average rainfall
value, perform error checking, and will return a float. */
{
    printf ( "Function 2\n" );
    if ( j >= 1 && j <= 100 )
    return j;
    else
        printf ( "Out of range: %f\n", j);

Quote:
}

float annualAvg ( float f, float g, float h, float i ) /* Function 2: Add
and average 4 floats as parameters and will return a float. */
{
    return (( f + g + h + i ) / 4);

Quote:
}

void endOfYearTotals ( float a, float b, float c, float d, float e ) /*
Function 3: Display the four quarterly values and the average, and return
nothing. */
{
    {
    printf ( "\n\nTotal values are:\n\n" );
    printf ( "First Quarter: %f\n", a );
    printf ( "Second Quarter: %f\n", b );
    printf ( "Third Quarter: %f\n", c );
    printf ( "Fourth Quarter: %f\n", d );
    printf ( "Annual Average: %f\n", e );
    }
    return ;
Quote:
}



Sun, 03 Apr 2005 23:32:28 GMT  
 Function not returning a correct value

Quote:
> Hi all,
> I'm trying to get Function 2 to work, but it doesn't. I got Function 1 to
> work just fine. Whats wrong with Function 2? I haven't got to Function 3
> yet....
> Thanks,
> Mike

Your Function 2 is fine, but the way you are calling it isn't.

(snip assignment description)

Quote:
> # include <stdio.h>
> # define SIZE 4
> float avgRainInput ( float );
> float annualAvg ( float, float, float, float );
> void endOfYearTotals ( float, float, float, float, float );
> int main ()
> {
>     float rain[ SIZE ];
>     int q = 1;
>     float k;
>     printf ( "Rainfall Calculator\n");
>     while ( q <= SIZE ) {
>     printf ( "Enter rainfall value for Quarter %d\n", q );
>     scanf ( "%f", &k );
>     avgRainInput( k );
>     rain[ q - 1 ] = k;
>     q++;
>     }
>     annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );
>     printf ( "annualAvg = %f\n", annualAvg );

This isn't the way you are supposed to call your functions.
The calls to avgRainInput and annualAvg compute the needed values, and
then throw them away. You then print the address of annualAvg as the
annual average.
You should instead do something like this:

printf("Enter rainfall value for Quarter %d\n", q);
scanf("%f", &k);
rain[q-1] = avgRainInput(k);
q++;

and then...

k = annualAvg(rain[0], rain[1], rain[2], rain[3]);
/* note the 0-based indexing */
printf("annualAvg = %f\n", k);

This is all about the basic function call syntax of the C language.

Quote:
> }
> float avgRainInput ( float j ) /* Function 1: Get one average rainfall
> value, perform error checking, and will return a float. */
> {
>     printf ( "Function 2\n" );
>     if ( j >= 1 && j <= 100 )
>     return j;
>     else
>         printf ( "Out of range: %f\n", j);

If your variable j is out of range, the return value of this function
will be indeterminate, and thus useless. You should return some sort
of "safe" value here, such as 1 or 100, or whatever suits you.

Quote:
> }
> float annualAvg ( float f, float g, float h, float i ) /* Function 2: Add
> and average 4 floats as parameters and will return a float. */
> {
>     return (( f + g + h + i ) / 4);
> }

This function is fine.

Quote:
> void endOfYearTotals ( float a, float b, float c, float d, float e ) /*
> Function 3: Display the four quarterly values and the average, and return
> nothing. */
> {
>     {
>     printf ( "\n\nTotal values are:\n\n" );
>     printf ( "First Quarter: %f\n", a );
>     printf ( "Second Quarter: %f\n", b );
>     printf ( "Third Quarter: %f\n", c );
>     printf ( "Fourth Quarter: %f\n", d );
>     printf ( "Annual Average: %f\n", e );
>     }
>     return ;
> }

This function looks OK. You don't need the inner set of braces or the
explicit return at the end.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
   - Douglas Adams



Mon, 04 Apr 2005 01:22:33 GMT  
 Function not returning a correct value

<snip>

Quote:
> # include <stdio.h>
> # define SIZE 4

> float avgRainInput ( float );
> float annualAvg ( float, float, float, float );
> void endOfYearTotals ( float, float, float, float, float );

> int main ()
> {

>     float rain[ SIZE ];
>     int q = 1;
>     float k;

float avgOut; /* you'll need this in a minute. */

Quote:

>     printf ( "Rainfall Calculator\n");
>     while ( q <= SIZE ) {
>     printf ( "Enter rainfall value for Quarter %d\n", q );
>     scanf ( "%f", &k );
>     avgRainInput( k );

What if avgRainInput() decides that this value isn't in range? Your
program warns the user, but does nothing else to avoid the value from
being used.

Quote:
>     rain[ q - 1 ] = k;
>     q++;
>     }
>     annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );

Change this to:

avgOut = annualAvg ( rain[ 0 ], rain[ 1 ], rain[ 2 ], rain[ 3 ] );

Quote:
>     printf ( "annualAvg = %f\n", annualAvg );

Change this to:

printf ( "annualAvg = "%f\n", avgOut);

Quote:
> }

> float avgRainInput ( float j ) /* Function 1: Get one average rainfall
> value, perform error checking, and will return a float. */
> {
>     printf ( "Function 2\n" );

Your comments call this function 1, but your printf calls it function 2.

Quote:
>     if ( j >= 1 && j <= 100 )
>     return j;
>     else
>         printf ( "Out of range: %f\n", j);
> }

If the number isn't in range, you don't return a value. If a function is
defined to return a value, it should return a value, irrespective of the
control path followed.

<snip>

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Mon, 04 Apr 2005 01:51:14 GMT  
 Function not returning a correct value
On 16 Oct 2002 17:22:33 GMT, in comp.lang.c , Joona I Palaste

Quote:


>> Hi all,
>>     annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );
>>     printf ( "annualAvg = %f\n", annualAvg );

>This isn't the way you are supposed to call your functions.

Out of interest, this is the 2nd time that I've seen someone do this
recently ie call a fn and expect it to magically store some value in
its own name. What language lets you do it, that people are confused?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Mon, 04 Apr 2005 07:24:16 GMT  
 Function not returning a correct value
On Wed, 16 Oct 2002 11:32:28 -0400, Michael

Quote:

>Hi all,
>I'm trying to get Function 2 to work, but it doesn't. I got Function 1 to
>work just fine. Whats wrong with Function 2? I haven't got to Function 3
>yet....

>Thanks,
>Mike

>/***************************************************************************
>                          assignment_5_problem_1.c  -  description
>                             -------------------
>    Student              : Michael
>    Class                : CIS 150 D
>    Date                 : Wed Oct 9 2002

>Instructor:

>This program will calculate the average rainfall for a year using the
>average rainfall reported for four quarters. The program will need to ask
>the user to enter an average rainfall for each quarter, add the amounts
>entered together and divide them by 4. The result should be output to the
>user. For this assignment you will need:

>    * A function that gets one average rainfall value. This function will
>use error checking so that no values less than 1 or greater than 100 will
>be accepted. The function will need no parameters and will return a float.

>    * A function that will add and average the 4 numbers entered into the
>program. This function will take 4 floats as parameters and will return a
>float.

>    * A function that will output the results. This function will display
>the four quarterly values and the average. It will return nothing.

>****************************************************************************/
># include <stdio.h>
># define SIZE 4

>float avgRainInput ( float );
>float annualAvg ( float, float, float, float );
>void endOfYearTotals ( float, float, float, float, float );

>int main ()
>{

>    float rain[ SIZE ];
>    int q = 1;
>    float k;

>    printf ( "Rainfall Calculator\n");
>    while ( q <= SIZE ) {
>    printf ( "Enter rainfall value for Quarter %d\n", q );
>    scanf ( "%f", &k );
>    avgRainInput( k );

Your assignment is to obtain the value in function-1.  Here you obtain
it in main and only check it in function-1.  Furthermore, the
requirement is that the function take no parameters.  You might want
to move the scanf call into the function.

Quote:
>    rain[ q - 1 ] = k;

And then change this to
        rain[q-1] = avgRainInput();

Quote:
>    q++;
>    }
>    annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );

This function is required to return a float.  You should not be
ignoring that value since you want to print it.  You might want
something like
        k = annualAvg(...);

Quote:
>    printf ( "annualAvg = %f\n", annualAvg );

The return value from a function is not stored in the function name.
In fact, using the function name like this simply passes a pointer to
the function to printf.  Not what you want.  If you make the change
recommended above, then simply use k which contains the value returned
by the function.

Why do you print the average value here and not call your third
function?

Quote:
>}

>float avgRainInput ( float j ) /* Function 1: Get one average rainfall
>value, perform error checking, and will return a float. */
>{
>    printf ( "Function 2\n" );

Actually, Function 1.

Quote:
>    if ( j >= 1 && j <= 100 )
>    return j;
>    else
>        printf ( "Out of range: %f\n", j);

This path produces an error by not returning a value.  The function is
defined to return a float and every exit path must do so.

Your assignment requires you to do more than merely check the range.
You must not accept a value out of range but rather obtain a new one
from the user.

- Show quoted text -

Quote:
>}

>float annualAvg ( float f, float g, float h, float i ) /* Function 2: Add
>and average 4 floats as parameters and will return a float. */
>{
>    return (( f + g + h + i ) / 4);
>}

>void endOfYearTotals ( float a, float b, float c, float d, float e ) /*
>Function 3: Display the four quarterly values and the average, and return
>nothing. */
>{
>    {
>    printf ( "\n\nTotal values are:\n\n" );
>    printf ( "First Quarter: %f\n", a );
>    printf ( "Second Quarter: %f\n", b );
>    printf ( "Third Quarter: %f\n", c );
>    printf ( "Fourth Quarter: %f\n", d );
>    printf ( "Annual Average: %f\n", e );
>    }
>    return ;
>}

<<Remove the del for email>>


Mon, 04 Apr 2005 10:00:14 GMT  
 Function not returning a correct value

Quote:

> On 16 Oct 2002 17:22:33 GMT, in comp.lang.c , Joona I Palaste


> >> Hi all,
> >>     annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );
> >>     printf ( "annualAvg = %f\n", annualAvg );

> >This isn't the way you are supposed to call your functions.

> Out of interest, this is the 2nd time that I've seen someone do this
> recently ie call a fn and expect it to magically store some value in
> its own name. What language lets you do it, that people are confused?

I'm pretty sure Pascal does.

-Micah
--
Now available for part-time and full-time permanent or contract
positions in the Sacramento, CA (USA) area (or telecommute). Yes, it's
my real email address.



Mon, 04 Apr 2005 10:54:32 GMT  
 Function not returning a correct value

Quote:


> > On 16 Oct 2002 17:22:33 GMT, in comp.lang.c , Joona I Palaste


> > >> Hi all,
> > >>     annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );
> > >>     printf ( "annualAvg = %f\n", annualAvg );

> > >This isn't the way you are supposed to call your functions.

> > Out of interest, this is the 2nd time that I've seen someone do this
> > recently ie call a fn and expect it to magically store some value in
> > its own name. What language lets you do it, that people are confused?

> I'm pretty sure Pascal does.

Pascal uses that notation instead of C's `return' statement.
There is no corresponding magical notation within the caller,
only within the called function.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons


Mon, 04 Apr 2005 12:51:06 GMT  
 Function not returning a correct value

Quote:


... snip ...

> > Out of interest, this is the 2nd time that I've seen someone do this
> > recently ie call a fn and expect it to magically store some value in
> > its own name. What language lets you do it, that people are confused?

> I'm pretty sure Pascal does.

No.  You are confusing it with the method of setting up a return
value in a function, by something like:

FUNCTION charfunction(char parameter) : char;
   BEGIN
   charfunction := 'A';
   (* more code *)
   END;

which can be done because assignment statements do not return a
value in Pascal.  Thus a recursive call on the right hand side of
an assignment is easily distinguished.  The caller would still do
something like:

   ch := charfunction(whatever);

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Mon, 04 Apr 2005 15:44:28 GMT  
 Function not returning a correct value


Quote:
> On 16 Oct 2002 17:22:33 GMT, in comp.lang.c , Joona I Palaste


> >> Hi all,
> >>     annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );
> >>     printf ( "annualAvg = %f\n", annualAvg );

> >This isn't the way you are supposed to call your functions.

> Out of interest, this is the 2nd time that I've seen someone do this
> recently ie call a fn and expect it to magically store some value in
> its own name. What language lets you do it, that people are confused?

The thing that never ceases to amaze me is that, when you get to writing
functions, the student has already been writing functions ( namely one
called main() ) and they have almost certainly been using a host of
functions from places such as the math library.

They have no problem writing something like:

int main(void)
{
    double x, y;

    for(x = 0.0; x < PI; x += 0.1)
    {
        y = sin(x);
        printf("sin(%3.1f) = %5.3\n", x, y);
    }

    return(0);

Quote:
}

Yet if you ask them to write a function called my_sin() that behaves just
like sin() and to redo the above task, many of them will struggle with how
to use the function in their new main() - no matter how much you emphasize
that programmer defined functions are used just like any of the other
function that they have already been using.


Mon, 04 Apr 2005 19:09:36 GMT  
 Function not returning a correct value

Quote:



> > > On 16 Oct 2002 17:22:33 GMT, in comp.lang.c , Joona I Palaste


> > > >> Hi all,
> > > >>     annualAvg ( rain[ 1 ], rain[ 2 ], rain[ 3 ], rain[ 4 ] );
> > > >>     printf ( "annualAvg = %f\n", annualAvg );

> > > >This isn't the way you are supposed to call your functions.

> > > Out of interest, this is the 2nd time that I've seen someone do this
> > > recently ie call a fn and expect it to magically store some value in
> > > its own name. What language lets you do it, that people are confused?

> > I'm pretty sure Pascal does.

> Pascal uses that notation instead of C's `return' statement.
> There is no corresponding magical notation within the caller,
> only within the called function.

Yes, I know. Sorry, I have to admit now that I didn't read anything
beyond Mark's question...  :-o

-Micah
--
Now available for part-time and full-time permanent or contract
positions in the Sacramento, CA (USA) area (or telecommute). Yes, it's
my real email address.



Tue, 05 Apr 2005 08:00:39 GMT  
 Function not returning a correct value

Quote:

> Hi all,
> I'm trying to get Function 2 to work, but it doesn't. I got Function 1
> to work just fine. Whats wrong with Function 2? I haven't got to
> Function 3 yet....

I just wanted to say "Thanks" to all for the advice. As I saw in an
earlier post:

Quote:
> The thing that never ceases to amaze me is that, when you get to writing
> functions, the student has already been writing functions ( namely one
> called main() ) and they have almost certainly been using a host of
> functions from places such as the math library.

> They have no problem writing something like:

And I have to say thats the truth. I think most students get stumped with
the way functions pass values. A month later from this post, and I'm
doing *much*, *much* better with C programming. On to pointers and
formatted I/O! Whee!!! :-)

-Mike



Sun, 08 May 2005 14:07:43 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. function return value not correct.

2. not returning correct value

3. RichEdit GetLineCount not returning correct value

4. CDC::DrawText doesn't return correct RECT values

5. Edit control fails to return correct value.

6. MethodBase.Invoke doesn't return the correct value

7. Returning error values vs. not returning

8. Not All Control Paths Return A Value/ Everything Is Not Valid

9. Debugger does not show correct values

10. Debugger does not show correct values

11. CInternetFile GetLength does not return correct size

12. function parameters and return values aren't changed by function call

 

 
Powered by phpBB® Forum Software