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

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

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

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.

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;
}

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.

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.

So the { } are super important in functions?

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.

Cool thanks for your help

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

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