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 trial

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

Lewis Davidson
Lewis Davidson
694 Points

Help of intro to IOS C questions with FLOAT use.

Question: 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.)

My Answer: int addTwo(int a, int b) { return a + b; }

What it said I did wrong: 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.

3 Answers

You're using floats, when you should be using ints. A float will append a decimal to both a and b, as opposed to an int which is only a whole number. The difference isn't that great when adding to values, but say you we're multiplying or dividing.

If I had a party and wanted to average my turn out for 3 different parties, with 15, 14, and 12 people. I wouldn't want a decimal value. It doesn't make sense to say my parties averaged 13.6 people. With integers, they truncate the decimal value, and typically round downwards or upwards (I think it varies upon language?), and if that language uses actual types.

Edit: A bit late, but who cares.

Hi Lewis,

Your only problem is that your function has 2 int parameters and it also returns an int

The challenge instructions are requesting float instead.

Lewis Davidson
Lewis Davidson
694 Points

How would that code be written?

You would just replace the 3 int's that you have with float

Lewis Davidson
Lewis Davidson
694 Points

What is the definition of a float, what is it used for, not seeing a "search" function beyond the initial asking of the question. Thanks for your time and help Jason!

You're welcome.

Ok, so a float is a floating point number. In other words, a number with a decimal point like 34.9

It's just another data type like int except int is only for storing integers like -34 or 127.

So the challenge is simply saying that you can expect 2 floats to be passed into your function. And when you add 2 floats together you're also going to get a float, (ex. 5.7 + 4.2 = 9.9) so you have to make sure the return type is also a float

Have you solved the challenge already or did I confuse you earlier into thinking you need to put some actual float numbers in there?