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
Alexander Bakushkin
5,120 PointsFunction in C
Hello!
I'm sorry, but I newbie in C, and I don't understand why I need to declare empty function at first and in the end of code I need to declare function with attributes, like this:
int funky_math(int a, int b);
int main()
{
int foo = 24;
int bar = 35;
int result = funky_math(foo, bar);
printf("funky math %d\n", result);
return 0;
}
int funky_math(int a, int b) {
return a + b + 123;
}
Why I can't write:
int funky_math(int a, int b) {
return a + b + 123;
}
int main()
{
int foo = 24;
int bar = 35;
int result = funky_math(foo, bar);
printf("funky math %d\n", result);
return 0;
}
? Thank you for your help and sorry for my English:)
2 Answers
Amit Bijlani
Treehouse Guest TeacherYou can write either however the first approach is recommended. Imagine a complex C program where you need to split functions up into multiple files. You can create a header file that contains only the definition of functions and a separate implementation file. When compiling the compiler just needs to include the header file which would be small. However, if you do not split up the header and implementation then the compiler would need to include all the implementation details which are not needed.
Alexander Bakushkin
5,120 PointsThank you, Amit! I'll try to learn more:)