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

Trouble with floats and functions in C

Anybody figure this out?

Write a function named "addTwo" that accepts two float numbers as arguments. The function should also return a float which is the sum of the two arguments passed to the function.

This is the best I've got but it's wrong

#include <stdio.h>

float addTwo();
int main()
{
    float a = 2.561;
    float b = 3.523;
    float addTwo = 'a' + 'b';

    return addTwo;

}

What am I doing wrong?

7 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

The code challenge is not asking you to call the function or print out the results. Your answer is simply:

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

Sweet!!! Thanks for the help. Now I can stop having OCD about it. I sincerely appreciate your help.

Is the indentation required? I know it's practice of good code, but I don't recall it ever being mentioned in any of the tutorials (since Xcode seems to do it for you). I had the following:

            <p>float addTwo(float a, float b) {
                  return a + b;
                  } </p>
            ``` 
and it kept returning an error.

sorry about the html job. Maybe I should try that course. ;-)

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

As long as you don't add the html to your answer it should work.

That did not copy and paste right.

Xcode likes my code but the quiz challenge doesn't like it. See the code below. What am I doing wrong?

#include <stdio.h>

float addTwo(float a, float b);
int main()
{
    float a = 2.561;
    float b = 3.523;
    float addTwo = a + b;
    printf("%f\n", addTwo);


    return 0;

}

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

FYI: You should add your replies as "replies" to your posts (see the "Comment" link right below the text you post, next to the time it was posted) as it adds the details and doesn't skew the "answer" count so people don't look at the question and think "Seven answers? The people here got this, I don't need to help!".

I followed these exact guidelines as shown in the video. How is this not the answer?

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

You don't need the main function because the challenge is not asking you to call the addTwo function or print the values.

Sorry my code isn't pasting correctly.

In the main() you have to set the parameters for your addTwo function.

I thought I did

float addTwo(float a, float b);
int main( )

Isn't this right? Did I do it right?

on line 3 after main(), your declaring a float variable with the same name as the function addTwo, and assigning that variable the sum of a and b. instead just call the function you've already created and just pass the variables to the function. I think you can do this like addTwo(a,b). Since it's returning the value, not storing it in a variable, you'll want to throw this into the printf() function.