Question:
How do I make mixed float/int function in C ?
anonymous
2013-07-01 15:21:09 UTC
Hello .. Well, I have this problem. Im trying to make a program in C which calculates area of a circle. You type in radius and it will calculate area. Well it was going pretty good, but Im stuck now because apparently one of the middleresults or middle calculations are float type. You know : PI*(r*r) is the formula to calculate area of circle. Well if r = 3 then 3*3 is 9. But when it comes to 9*PI, it happens to be a float type of number. Which I dont know how to put in a function. I was doing something like this :

#define PI 3.14

int radius;
int middleReslut;
float result;

printf("Enter radius : \n");
scanf("%d", &radius);
blah blah blah and then
calculation( radius , middleReslut , result);
return 0;
}

float calculation(int a, int b, float c) {
b = a*a;
c = b*PI;
printf("%f", c);
return 0;
}

I have problem in putting two different variable types into one function. Can anybody help me ? Thank you.
Three answers:
Ratchetr
2013-07-01 16:06:03 UTC
When you say 'it appears its still not working', what do you mean by not working?



Does your code compile, or do you get errors? If you get errors, what are they?



Does the code run, but produce the wrong result? If you input 3, what result do you get?



Your calculation function does print the right result. If you call it with a = 3, it will print 28.26, which seems right.



You really shouldn't be passing 3 parameters to calculation. It only needs 1: the radius.



The other 2 variables you have, b and c can be local variables to the function.



Also, since it is a function that returns a float, it really should return the result of the calculation (The area). Printing the result should be left to the caller.



Consider writing it more like this:

float area_of_circle(int radius)

{

    float b,area;

    b = radius * radius;

    area = b * PI;

    return area;

}



Call it like this:

printf("area=%f", area_of_circle(radius));
husoski
2013-07-01 23:21:10 UTC
Your example is incomplete, but it looks like the problem is lack of a prototype. Normally, a name can't be used in C until after it's been defined. So the main() function, appearing before calculation(), doesn't know what the arguments or result type are. You can fix that in either of two ways:



1. Move the definition of calculation() in front of main, and keep it ahead of any other functions that might use it.



2. Use a prototype. A prototype is a copy of the function header, minus the braced function body, used as a declaration of a function name, its arguments and return type. Before main() you could add:



float calculation(int a, int b, float c);



In your calculation() function, there's some oddness. You have an argument b, but you don't use its value. You use it as a local variable, instead. That value you compute for b won't get stored in the caller's variable. Functions only get copies of the arguments. If you need to return a value, use the return value to return something other than 0. (Later, you'll learn how to use pointers to return two or more different values. For now, though, concentrate on writing functions that return a single value.)
Sleeping Troll
2013-07-01 22:34:30 UTC
Just use float instead of integer, 3, 3.00 are the same thing!



float radius;

float middleReslut;

float result;



printf("Enter radius : \n");

scanf("%d", &radius);

blah blah blah and then

calculation( radius , middleReslut , result);

return 0;

}



float calculation(float a, float b, float c) {

b = a*a;

c = b*PI;

printf("%f", c);

return 0;

}



after all, the radius could be 153.37852798 couldn't it?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...