Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
maithreya reddy addula
3,761 PointsTrouble solving the Functions Code Challenge in learning C
Write a function named "addTwo" that accepts two float numbers as arguments. The function should also return a float which is the sum of the two arguments passed to the function.
#include <stdio.h>
int addTwo(float a, float b);
int main(){
float mom = 9.5;
float dad = 0.5;
printf("The myth is %d\n", addTwo(mom,dad));
return 0;
}
int addTwo(float a, float b){
return a + b;
}
Amit Bijlani can you please a look at it, when i am running it on my xCode its running fine.
i have one more question do you see in printf statement when i used %f i am getting some strange error but when i tried using %d it worked can you explain it.
5 Answers
Eddie Flores
9,110 PointsAll you need to write is:
float addTwo(float a, float b){
return a + b;
}
All it's asking for is the function to be given. In this case, what is above. Make sure that you have it as a "float" if not it won't work.
Edit: Just as a tip, even though you wrote out the whole thing, just make sure that you when you write it in your Xcode compiler, that you have the right associations. I see that you used a %d in your print out, for a float, be sure to use %f.
Eddie Flores
9,110 PointsYou are not printing a float. You have it set to print an int. This can cause errors. That's why you need to change:
%d to %f
and
int addTwo to float addTwo
John W
21,558 PointsIn code challenges, you only need to satisfy the question, think providing a snippet, or code example. Thus, all you need for this particular challenge is the function itself, i.e. the last 3 lines of your code. (except the function should be returning a float instead of int).
Good job constructing a fully functional program.
maithreya reddy addula
3,761 Pointsthanks guys by the way do you see %d in my printf in the above code. can any one explain me what's happening there when i put %f ?
sorry i was over thinking
Eddie Flores
9,110 PointsWhen using different types of variables, you need to use different letters to call them.
- Integers are called by %d
- chars are called by %c
- floats are called by %f
maithreya reddy addula
3,761 Pointsyeah but here i am using float function right and printing float.
maithreya reddy addula
3,761 Pointsthanks man i got it now
Taylor Hammons
1,609 PointsTaylor Hammons
1,609 PointsHow did you know to write that? The question, to me, seemed so vague. I followed the videos in xcode and read the attached readings, and everything, but I had no clue where to begin when I got to this challenge.