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 trialPaul Kitchener
886 PointsCan't get past iOS Development function challenge
I'm sure this is simple, but I can't figure it out:
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.)
6 Answers
Stone Preston
42,016 Pointshere is a shell of what your function should look like:
returnType functionName(dataType argumentOne, dataType argumentTwo) {
return something;
}
the challenge tells you what the return type should be, the function name, and the datatypes of the arguments. you can call the arguments (actually they are known as parameters in this case) whatever you want, just be consistent when referencing them. it says to return the sum of the arguments passed in to the function
Brenden Konnagan
16,858 PointsPaul... your function is wanting to return an "int" while taking in two "float" arguments. The way your code is written it is trying to return a "float" instead of an "int". If you need it to return an int, then you need to cast a & b as int.
Make sense?
Michael Blazek
162 PointsDo you have code that you have written and tried to submit? Maybe post that and we can help you through it.
Paul Kitchener
886 Pointsstill struggling with this. i'm new to code, and the terminology is confusing the heck out of me. here is what i tried that failed:
int addTwo (float a, float b);
{ float foo = 20; float bar = 30; printf("addTwo %d", addTwo(foo, bar)); }
int addTwo (float a, float b) { return a + b; }
Paul Kitchener
886 PointsWhoops, not sure how to copy/paste in actual code box. That spacing makes it confusing...
Michael Blazek
162 PointsYour very close. Look at your return type and ask why your returning an int.
Paul Kitchener
886 PointsOkay, I finally got it. Thanks for your help!
Steve B
6,257 PointsSteve B
6,257 PointsSurely this is what your saying, but it wont pass
Stone Preston
42,016 PointsStone Preston
42,016 PointsSteve B no, look again. you use parenthesis around the parameters, not brackets: