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 trialAndy Hanuman
1,435 PointsIm lost can somebody help me out on this Functions code challenge
Implement a function named "addTwo" that returns the sum of two floats. The function will accept two float numbers as arguments. It should add the two arguments together, and return the result. (No need to write the main function. Just write out the implementation for the addTwo function.)
i have int addTwo (int a , int b); { int foo = 30; int bar = 31; printf ("addTwo %f", addTwo(foo,bar)); }
16 Answers
Chris Atlas
2,038 PointsMy code isn't working. HELP
float addTwo (float One, float Two); { return float One + float Two; }
Stone Preston
42,016 Pointsyou are making the function a bit too complicated, and your syntax is a bit off as well. a function tends to look something like this:
returnType functionName (dataType argumentOne, dataType argumentTwo) {
return something;
}
in this case the returnType needs to be a float, the function needs to be named addTwo, it needs accept two float arguments (you can name them whatever) and it needs to return the sum of those arguments.
AbdelGhafour Mohie
1,876 Pointsuse -float- instead of -int- , And actually use the + sign to sum both variables.
Amy Hopkins
675 PointsDid you do it? I've been stuck on it for ages and tried what feels like every possible combination with no luck!!
Stone Preston
42,016 Pointscan you post the code you have been trying?
Amy Hopkins
675 PointsErm I got a little desperate so I've tried a lot of different things in the hope one of them will do something/anything!
Most of the stuff I tried was along the veins of;
int addTwo(float a, float b); int main() { float foo = 24; float bar = 36;
printf("%f\n", addTwo(foo, bar)); return 0;
} int addTwo(float a, float b) { return a + b; }
Most of the stuff looks like that but with variations on which is int and which is float. I tried really simple stuff as well but had no luck so just went mad and put everything in it I could do! Thank you for your help :)
Stone Preston
42,016 Pointsall you need to do is implement the function. you dont need main. you dont need the prototype. all you need is the function implementation which should look something like this
returnType functionName (dataType argumentOne, dataType argumentTwo) {
return something;
}
the challenge tells you the return type should be a float, the name should be addTwo, it should take two float arguments, and return the some of those arguments.
Amy Hopkins
675 PointsThanks for your patience, I did read your earlier answer but for some reason I just couldn't work it out. Finally done it, now it seems really obvious...!
Michael Rau
325 PointsI cannot figure this out. I'm not sure what I am missing. Here is the code i have so far:
float addTwo (float 0, float 1); {
return float 0 + float 1;
} Tried to simplify it, maybe it is too simple though.
Stone Preston
42,016 Points0 and 1 are not valid variable names (variable names cant be numbers). you have the right idea, you just need to change float 0 and float 1 to something like float floatOne and float floatTwo. also in the body of the function you can just reference them as floatOne and floatTwo, you dont need to data type in front since they are already defined as floats above
Ben Holland
Courses Plus Student 4,062 Pointsfloat addTwo (float One, floatTwo); {
return One + Two;
}
Why doesnt this work ? Please help
Stone Preston
42,016 Pointsthats almost correct, however your second parameter isnt quite right. you need a space between the data type and the parameter name:
float addTwo (float One, float Two); {
return One + Two;
}
also, generally its best to begin your variables with lowercase letters and use camel case ie float one, float two, float someFloat, etc
Ben Holland
Courses Plus Student 4,062 PointsOhhh ok thank you !
Michael Rau
325 Pointsfloat addTwo (float One, float Two);{
return One + Two;
} I used this code and even tried in Xcode. Both say expected identifier '('
I am not sure what this means.
Stone Preston
42,016 Pointsyou need to remove the semicolon after the function parameters:
float addTwo (float One, float Two) {
return One + Two;
}
Benjamin Stanton
1,398 PointsIn the videos, when did we ever learn "return" ?
Stone Preston
42,016 Pointshe starts talking about the return statement at 2:43 of this video
Benjamin Stanton
1,398 PointsThanks for that. After all day at work, apparently 3 hours of this stuff is about when my brain stops learning new things, lol. Re-watched the video a couple times, I'm finally at a point where I think I need to do a little more independent study.
dsv97sdf789
2,166 PointsIn those two videos, I didn't hear him say anything about floats. I'm confused. Can anyone explain? Thanks much.
Melvin Ho YK
Courses Plus Student 25,247 PointsYou need to go back to Variables lesson under Fundamentals of C at 1:50. Good luck!
karan patel
1,046 Points//i dont know anything
Melvin Ho YK
Courses Plus Student 25,247 PointsAfter checking on what I had typed originally:
float addTwo (a,b) {
return a + b;
}
(incorrect) To checking on answers here which were no direct answer but few changes and checking on my notes. Done, here you go:
float addTwo (float One, float Two) {
return One + Two;
}
Thanks @Stone Preston for the explanation given above plus my note written from the tutorial:
//sample code reference
int funky_math (int a, int b) {
return a + b + 343;
}
Kevin Baier
2,597 PointsI enter this and it doesn't work. Why not? Flumoxxed...
float addTwo = (float K, float M)
{
return K + M
}
GABRIEL THE
226 PointsThis worked for me:
float addTwo (float a, float b){ return a + b; }
David Birnie
2,726 PointsI feel like at this point in the course I am doing I should understand what I am learning... but frankly I am getting more lost.... I have re watched videos, re taken tests and am getting more frustrated... is there a better way to learn this?
Zach Thacker
2,089 Pointsjust eat spaghetti
Zach Thacker
2,089 Pointsjust eat spaghetti
Zach Thacker
2,089 Pointsjust eat spaghetti
Chris Atlas
2,038 PointsChris Atlas
2,038 Pointsnever mind, I got it!