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 trialBrian Darby
319 PointsWhats wrong with my code.
I dont know whats wrong with my code can some body explain what i have done wrong thx.
int addTwo(float a, float b); {
a= 1.23
b= 3.56
return a+b;
}
1 Answer
Matthew Young
5,133 Points(Forgot that adding a comment isn't submitting an answer to the question)
There are some syntax errors with your code. However, to start, you don't want to specify values for your parameters within the function. Even if you could assign them to your parameter variables, your code would essentially only calculate the sum of 1.23 and 3.56 no matter what you passed through each time it is called. That's not very useful unless you want the same answer each time.
If the purpose of your code is just to add two numbers together, you only want the following bit of code:
float addTwo(float a, float b) {
return a + b;
}
When you call that function, only then would you pass through the values you wish to have added together. In the declaration, you're just defining what to do with the values once passed through.
(Side note: Your assignment this is based off of asks for a return type of "float" so I changed that from "int" in my code to follow the requirements of the assignment. Even if I kept the "int" return type, your answer would be inaccurate because an "int" doesn't include decimals so 1.23 + 3.56 would be 4 if returned as an "int". Also, don't forget that each line of code you write needs a semi-colon at the end. You don't need it after the parameters in your function declaration, but you do need it, for instance, after the return statement.)
I hope this helps you out! Please let me know if you want any further clarification.