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

Roger Green
Roger Green
643 Points

Function

im suppose to create a function that returns a float. I don't know were I went wrong. int addTwo (float a, float b); float goku = 4.4; float vegeta = 6.6; printf("addTwo %f", addTwo(goku, vegeta));

int addTwo (float a, float b); return a + b + 7;

6 Answers

 Vladimir Cezar
 Vladimir Cezar
7,710 Points

If a function returns a float it needs to be be declared as it. So, instead of

int addTwo (float a, float b);

you should write

float addTwo (float a, float b);
Roger Green
Roger Green
643 Points

I tried what you said but it continues to say this:

expected function body after function declarator return a + b + 7.8; ^ 2 errors generated.

 Vladimir Cezar
 Vladimir Cezar
7,710 Points

Your function declaration is also wrong. here is the full, corrected, code:

int main(int argc, const char * argv[])
{
    float addTwo (float a, float b);
    float goku = 4.4;
    float vegeta = 6.6;
    printf("addTwo %f", addTwo(goku, vegeta));
    return 0;
}

float addTwo (float a, float b){
    return a + b + 7;
}
Roger Green
Roger Green
643 Points

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

This is the main goal. I tried what you showed me but it came up with the same error.

 Vladimir Cezar
 Vladimir Cezar
7,710 Points

Ok, here it is.

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

Copy and paste exactly what is above and ONLY that. It works.

Roger Green
Roger Green
643 Points

So the { } are super important in functions?

 Vladimir Cezar
 Vladimir Cezar
7,710 Points

Yes. Curly brackets define a block of code. Without it an C function would not know where to start or begin. Please consider to accept the answer.

Roger Green
Roger Green
643 Points

Cool thanks for your help

 Vladimir Cezar
 Vladimir Cezar
7,710 Points

If this answer your question would you please mark the answer as 'Best answer'? It helps with my scoring. Thanks.

 Vladimir Cezar
 Vladimir Cezar
7,710 Points

Sure. Don't forget to mark the answer as *'Best Answer'*