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

Atiba Boswell
Atiba Boswell
9,960 Points

Error in Functions Code Challenge - Functional Programming in C

Can someone explain why I get this error and give example of the correct code:

conflicting types for 'main' int main(int argc, const char * argv[]) ^ note: previous definition is here int main() ^ 1 error generated.

The challenge reads: 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.

5 Answers

Magnus Hovland
Magnus Hovland
5,657 Points

Could it be that you include the "main()" function in your answer code as well? I tested it now and got the same error as you describe here.

When I try the following, it passes (without declaring the main function):

float addTwo(float a, float b);

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

Edit: Btw, I don't think this is the best way to do it, but it works. :)

The main function is what you execute from when you run a C program, but you can also code up a function without a main, compile it, and then use that function in some another C program with a main by linking them.

Magnus' solution is correct, though you can also do it without the first line. The first line is usually placed in the header or before the main function so that the compiler knows about it before it reads the main body of your code.

Magnus Hovland
Magnus Hovland
5,657 Points

Thank you for explaining this further. Think I understand the role of the different parts a bit better now. :)

Atiba Boswell
Atiba Boswell
9,960 Points

Thanks Magnus, that works.

But I thought the main function and #include <stdio.h> was needed for all C programs?

Was my error just because the code challenge asks for only one specific part of the code?

Thanks for the help. I am a beginner to C and programming in general and really want to understand each concept.

Magnus Hovland
Magnus Hovland
5,657 Points

I'm quite new to C myself, but I believe you are right about the main function being a requirement.

For the challenges, however, my guess is that they have simplified it so you only need to concentrate on making the actual function they ask for. So the main function is already included "behind the scenes".

So I guess you really didn't do anything wrong, you just did a little more that the challenge asked for. And since there can only be one main function, it blew up when you put in that second one. That's my theory anyhow. I'm sure there is someone here who can tell us if this is actually the case or not.

Keep up the good work :) -M

Atiba Boswell
Atiba Boswell
9,960 Points

Awesome thanks guys. I was stuck for a few days, excited to move on. Next time i'll use the forum asap.