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

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!