Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Help a brudda out

Name: Anonymous 2012-04-06 23:15

Does anyone know how to use functions in c? I keep getting garbage values when I run the program

#include<stdio.h>
#include<math.h>
#define PI 4*atan(1)

    double circle(double radius)
{
        double area;
        area=radius*radius*PI;
        return area;
}

    double sphere(double radius)
{
        double volume;
        volume=(radius*radius*radius)(PI(3/4));
        return volume;
}

main()
{
double radius;

printf("Enter radius:");
scanf("%lf", &radius);

printf("\n %6.3lf \n %6.3lf", circle, sphere);
return 0;
}

Name: Anonymous 2012-04-07 8:54

You need to actually pass in your main's radius double into the function calls.

printf("\n %6.3lf \n %6.3lf", circle(radius), sphere(radius));
However, I don't think you're gonna want to display the circle and sphere numbers right after the other like this though so more like

printf("\n\nCircle area:", circle(radius),"\nSphere volume:", sphere(radius));

I don't know what %6.3lf is supposed to be though.

Name: Anonymous 2012-04-08 22:25

yeah you wanna actually pass values to your function, in this case your radii

Name: Anonymous 2012-04-12 14:58

you are passing the value of symbols of the function type, which means you're printing a floating value representation of function memory addresses, not what you want.

what you want is to make symbols to hold the circle and sphere values and call those functions

double circle_val = circle_func(radius);
printf("\n %6.3lf \n %6.3lf", circle_val, sphere_val);

Don't change these.
Name: Email:
Entire Thread Thread List