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

Andy Hanuman
Andy Hanuman
1,435 Points

Im lost can somebody help me out on this Functions code challenge

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

i have int addTwo (int a , int b); { int foo = 30; int bar = 31; printf ("addTwo %f", addTwo(foo,bar)); }

16 Answers

Chris Atlas
Chris Atlas
2,038 Points

My code isn't working. HELP

float addTwo (float One, float Two); { return float One + float Two; }

Chris Atlas
Chris Atlas
2,038 Points

never mind, I got it!

Stone Preston
Stone Preston
42,016 Points

you are making the function a bit too complicated, and your syntax is a bit off as well. a function tends to look something like this:

returnType functionName (dataType argumentOne, dataType argumentTwo) {

return something;

}

in this case the returnType needs to be a float, the function needs to be named addTwo, it needs accept two float arguments (you can name them whatever) and it needs to return the sum of those arguments.

use -float- instead of -int- , And actually use the + sign to sum both variables.

Did you do it? I've been stuck on it for ages and tried what feels like every possible combination with no luck!!

Stone Preston
Stone Preston
42,016 Points

can you post the code you have been trying?

Erm I got a little desperate so I've tried a lot of different things in the hope one of them will do something/anything!

Most of the stuff I tried was along the veins of;

int addTwo(float a, float b); int main() { float foo = 24; float bar = 36;

printf("%f\n", addTwo(foo, bar)); return 0;

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

Most of the stuff looks like that but with variations on which is int and which is float. I tried really simple stuff as well but had no luck so just went mad and put everything in it I could do! Thank you for your help :)

Stone Preston
Stone Preston
42,016 Points

all you need to do is implement the function. you dont need main. you dont need the prototype. all you need is the function implementation which should look something like this

returnType functionName (dataType argumentOne, dataType argumentTwo) {

return something;

}

the challenge tells you the return type should be a float, the name should be addTwo, it should take two float arguments, and return the some of those arguments.

Thanks for your patience, I did read your earlier answer but for some reason I just couldn't work it out. Finally done it, now it seems really obvious...!

I cannot figure this out. I'm not sure what I am missing. Here is the code i have so far:

float addTwo (float 0, float 1); {

return float 0 + float 1;

} Tried to simplify it, maybe it is too simple though.

Stone Preston
Stone Preston
42,016 Points

0 and 1 are not valid variable names (variable names cant be numbers). you have the right idea, you just need to change float 0 and float 1 to something like float floatOne and float floatTwo. also in the body of the function you can just reference them as floatOne and floatTwo, you dont need to data type in front since they are already defined as floats above

Ben Holland
PLUS
Ben Holland
Courses Plus Student 4,062 Points

float addTwo (float One, floatTwo); {

return One + Two;

}

Why doesnt this work ? Please help

Stone Preston
Stone Preston
42,016 Points

thats almost correct, however your second parameter isnt quite right. you need a space between the data type and the parameter name:

float addTwo (float One, float Two); {

return One + Two;

}

also, generally its best to begin your variables with lowercase letters and use camel case ie float one, float two, float someFloat, etc

float addTwo (float One, float Two);{

return One + Two;

} I used this code and even tried in Xcode. Both say expected identifier '('

I am not sure what this means.

Stone Preston
Stone Preston
42,016 Points

you need to remove the semicolon after the function parameters:

float addTwo (float One, float Two) {

    return One + Two;
}
Benjamin Stanton
Benjamin Stanton
1,398 Points

In the videos, when did we ever learn "return" ?

Stone Preston
Stone Preston
42,016 Points

he starts talking about the return statement at 2:43 of this video

Benjamin Stanton
Benjamin Stanton
1,398 Points

Thanks for that. After all day at work, apparently 3 hours of this stuff is about when my brain stops learning new things, lol. Re-watched the video a couple times, I'm finally at a point where I think I need to do a little more independent study.

In those two videos, I didn't hear him say anything about floats. I'm confused. Can anyone explain? Thanks much.

You need to go back to Variables lesson under Fundamentals of C at 1:50. Good luck!

//i dont know anything

After checking on what I had typed originally:

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

(incorrect) To checking on answers here which were no direct answer but few changes and checking on my notes. Done, here you go:

float addTwo (float One, float Two) {
      return One + Two;
}

Thanks @Stone Preston for the explanation given above plus my note written from the tutorial:

//sample code reference
int funky_math (int a, int b) {
     return a + b + 343;
}
Kevin Baier
Kevin Baier
2,597 Points

I enter this and it doesn't work. Why not? Flumoxxed...

float addTwo = (float K, float M)

{

return K + M

}

GABRIEL THE
GABRIEL THE
226 Points

This worked for me:

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

David Birnie
David Birnie
2,726 Points

I feel like at this point in the course I am doing I should understand what I am learning... but frankly I am getting more lost.... I have re watched videos, re taken tests and am getting more frustrated... is there a better way to learn this?

just eat spaghetti

just eat spaghetti

just eat spaghetti