Function Help

How to return double to main?

Page 1 of 1

9 Replies - 900 Views - Last Post: 23 February 2010 - 05:34 PM Rate Topic: ***-- 1 Votes

#1 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Function Help

Posted 23 February 2010 - 02:09 PM

I am passing 3 int arguments into a function. And in the function I am calculating the average. How would do I return the output (average) as a double into the main? Hope this question makes sense. My output now comes out as 0.0000. What am I doing wrong?

Thanks for the helpful replies :)

#include <stdio.h>
#include <stdlib.h>

int funcAverage( int x, int y, int z ){
	double sumTotal, average;

	sumTotal = x + y + z;
	average = sumTotal / 3;


	return average;
}


main(){
int value1 = 9, value2 = 5 , value3 = 12, average1;


average1 = funcAverage( value1, value2, value3 );
printf("\nThe first average is: %.4lf\n\n", average1);


	system("pause");

}

This post has been edited by sf18: 23 February 2010 - 02:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Function Help

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Function Help

Posted 23 February 2010 - 02:14 PM

You're intermixing ints and doubles.


You're using variables that aren't declared and then printing out values that have no meaningful value. [assign the return of the function to average1]
Was This Post Helpful? 0
  • +
  • -

#3 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Function Help

Posted 23 February 2010 - 02:23 PM

@KYA

Not quite sure if I understand your answer, but I made a few typos in my code and just edited it. I'm still getting 0.0000. Any other suggestions.
Was This Post Helpful? 0
  • +
  • -

#4 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Function Help

Posted 23 February 2010 - 02:24 PM

You return a double simply by specifying the return type of the function as double.

double funcAverage( int x, int y, int z ){
        double sumTotal, average;

        sumTotal = x + y + z;
        average = sumTotal / 3;


        return average;
}



Keep in mind that you can return just about any data type, like strings, char's, pointers etc...

Another thing to keep in mind is that typecasting will allow you to force the variable to a double if you wish. Here is an example

int num = 5;
printf("%f", (double)num / 2);
// Or you can typecast this way...
printf("%f", double(num) / 2);

This post has been edited by Aphex19: 23 February 2010 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Function Help

Posted 23 February 2010 - 02:30 PM

@Aphex19

Here is my printf:
printf("\nThe first average is: %.4lf\n\n", (double)average1);


But now it outputs as: 8.0000 and it should be 8.6667.

??
Was This Post Helpful? 0
  • +
  • -

#6 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Function Help

Posted 23 February 2010 - 02:41 PM

For two main reasons.

1) you're averaging function is returning in integer when it needs to return a double.

2) The variable you are declaring to hold the return value of you're averga function is an integer. It should be a double as it needs to hold a double.

One more thing, it is good practice to return 0 from the main.

#include <stdio.h>
#include <stdlib.h>

// This function needs to return a double
double funcAverage( int x, int y, int z )
{
    double sumTotal, average;

    sumTotal = x + y + z;
    average = sumTotal / 3;

    return average;
}


int main()
{
    int value1 = 9, value2 = 5 , value3 = 12;
    double average1;    // Averge1 must be a double as it takes the return value of funcAverage

    average1 = funcAverage( value1, value2, value3 );
    printf("\nThe first average is: %.4lf\n\n", average1);

    system("pause");
    return 0;
}



EDIT: changed printf, doesnt need to typecast to double anymore.

This post has been edited by Aphex19: 23 February 2010 - 02:45 PM

Was This Post Helpful? 1
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Function Help

Posted 23 February 2010 - 02:46 PM

really your function can just be simplified to
// This function needs to return a double
double funcAverage( int x, int y, int z )
{
	return (double)(x+y+z)/3;
}


but either way works :)
Was This Post Helpful? 1
  • +
  • -

#8 sf18   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Function Help

Posted 23 February 2010 - 02:47 PM

@Aphex19

OoOoOooo okay thanks for the break down. I very much appreciate it!

:)
Was This Post Helpful? 0
  • +
  • -

#9 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Function Help

Posted 23 February 2010 - 02:53 PM

View Postsf18, on 23 February 2010 - 01:47 PM, said:

@Aphex19

OoOoOooo okay thanks for the break down. I very much appreciate it!

:)


you're welcome ^^
Was This Post Helpful? 0
  • +
  • -

#10 Guest_c.user*


Reputation:

Re: Function Help

Posted 23 February 2010 - 05:34 PM

printf("\nThe first average is: %.4lf\n\n", average1);


printf uses %f for float and double
it uses %Lf for long double
Was This Post Helpful? 0

Page 1 of 1