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

Can't get past iOS Development function challenge

I'm sure this is simple, but I can't figure it out:

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

6 Answers

Stone Preston
Stone Preston
42,016 Points

here is a shell of what your function should look like:

returnType functionName(dataType argumentOne, dataType argumentTwo) {

return something;

}

the challenge tells you what the return type should be, the function name, and the datatypes of the arguments. you can call the arguments (actually they are known as parameters in this case) whatever you want, just be consistent when referencing them. it says to return the sum of the arguments passed in to the function

Steve B
Steve B
6,257 Points

Surely this is what your saying, but it wont pass

float addTwo[float a, float b]{
return a + b;
}
Stone Preston
Stone Preston
42,016 Points

Steve B no, look again. you use parenthesis around the parameters, not brackets:

float addTwo(float a, float b) {
return a + b;
}
Brenden Konnagan
Brenden Konnagan
16,858 Points

Paul... your function is wanting to return an "int" while taking in two "float" arguments. The way your code is written it is trying to return a "float" instead of an "int". If you need it to return an int, then you need to cast a & b as int.

Make sense?

Do you have code that you have written and tried to submit? Maybe post that and we can help you through it.

still struggling with this. i'm new to code, and the terminology is confusing the heck out of me. here is what i tried that failed:

int addTwo (float a, float b);

{ float foo = 20; float bar = 30; printf("addTwo %d", addTwo(foo, bar)); }

int addTwo (float a, float b) { return a + b; }

Whoops, not sure how to copy/paste in actual code box. That spacing makes it confusing...

Your very close. Look at your return type and ask why your returning an int.

Okay, I finally got it. Thanks for your help!