Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Functional Programming in C Functions

Whats wrong with my code.

I dont know whats wrong with my code can some body explain what i have done wrong thx.

functions.c
int addTwo(float a, float b); {
  a= 1.23
  b= 3.56
  return a+b;
}

1 Answer

Matthew Young
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.