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 trialDavid Kong
3,710 PointsStuck on code challenge
I'm not sure where I'm going wrong... I'm having trouble translating what was in the videos, into what is being asked for in this code challenge. I'm also a little thrown off, if I'm getting this wrong by not having a Main function, although it says I don't need one...
The instructions are:
Implement a function named "addTwo" that returns the sum of two floats. The function will accept two float numbers as arguments and return a float which is the sum of the two arguments passed to the function. (No need to write the main function just write out the implmentation for the addTwo function)
The error message I get is:
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.
The URL is:
https://teamtreehouse.com/library/functions
Thanks, Dave
// Declare initial function
int addTwo(int a, int b);
// Perform function
int addTwo(int a, int b)
{
//Initialize variables
float a = 1.1;
float b = 2.2;
//Return result
return a + b;
}
3 Answers
RASHU BHATNAGAR
Courses Plus Student 2,669 PointsHi Dave,
You just have to implement the function like this :
float addTwo(float a, float b) { return a+b; }
Ok let me explain a little bit. You would be implementing the addTwo function outside the main function, In the main function you would be the passing the values of a and b (which would be float values) into the addTwo function which would be returning the addition of a and b back to the main function where it was called.
More over the editor is expecting you to declare a and b to be float values and not int.
I hope it helps you a little bit in understanding...
David Kong
3,710 PointsHi Rashu - thanks a lot, that worked like a charm...
Ok, so basically I was doing a few things wrong.
1 - I needed to initialize the function as a float... But I thought INT was just basically short for "initialize"... Do I instead initialize the function based on the TYPE of result I'm returning??
If variable "a" or "b" was going to be a char or string, would I have done "char addTwo(char a, char b)"
2 - I didn't need to set values to the variables inside my function, since the purpose of the function is to perform an operation - add... I guess the main() function would be passing me the actual values.
3 - And I also needed to declare the variables as floats...
Mostly curious the answer to Q1
Thanks, Dave
Daniel Burt
3,699 PointsEven though I didn't ask the original question, Rashu has solved the issues I was having myself - thanks ever so much.
I am a relative coding newbie, and I do wonder whether the wording of the challenge isn't a little bit confusing based on what is said in the videos previous to the challenge as I was doing a very similar thing to David.