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 trialRick Wolchuk
2,063 PointsStuck on a functions question
Hello
Can you help me out? I'm stuck on a tree house question and I was wondering if you could give me an assist on this?
The questions is asking:
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.)
This is what I entered:
int addTwo(float a, float b); { float a = 2; float b = 3; } int addTwo(float a, float b) { return a + b; }
The problem doesn't satisfy the Treehouse editor and it's giving me an error statement:
Bummer! Make sure you've defined the return type and both paramter types as 'float' and that you are returning the sum of the arguments as a result What am I doing wrong? I'm not finding the mistake.
Sincerely, Rick
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Rick,
The challenge only wants the implementation of the function which is the last part of your code. Also, the function should return a float and you have it returning an int.
float addTwo(float a, float b) {
return a + b;
}
This is the implementation of the function or sometimes referred to as the function definition. You were trying to create a complete program and doing too much.
Rick Wolchuk
2,063 PointsHey Jason,
Thank you so much! I appreciate your getting back to me so quickly on this.
Have a great weekend, Rick
Jason Anello
Courses Plus Student 94,610 PointsYou're welcome. Have a great weekend too.
Abdulla Alshubbar
31,298 PointsHello Rick,
When creating a function, you need to start it with the keyword "function". Also, you don't need to specify the type of the parameters the function takes. Just the names are fine. So it should look like this:
function AddTwo(a,b){ //code goes here };
This is for Java.
But for ios, it is okay to use float instead of function.
Jason Anello
Courses Plus Student 94,610 PointsHi Abdulla,
Maybe you're thinking javascript?
Jason Anello
Courses Plus Student 94,610 PointsDidn't see your edit yet.