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

Michael McLaughlin
Michael McLaughlin
14,033 Points

obj c basics > functional programming in c > functions

just wondering why douglass put the line

int funky_math(int a, int b);

before the int main function. i tried commenting only that line out and it ran, but it threw a warming, 'implicit declaration of function "funky_math" is invalid in C99' just wondering if anyone can explain what this means or if there is a particular reason we put the previous line of code at the top of the .c file.

4 Answers

Stone Preston
Stone Preston
42,016 Points

That line is a function prototype. It lets the compiler know about the type/number of arguments etc the function has. you should always declare a function prototype or at least define your function before you use it . If you dont, the compiler "guesses" a prototype based on the parameters you pass to the function. This can cause lots of problems in many cases.

I dont think you have to supply a prototype, just a function definition before you call it. But normally its easier to just provide the prototype first thing and not have to worry

This page might be helpful

We always need to either declare or define the functions before the main function. The reason behind this is that when the program run and comes across the function in the main function, and if we have not either declared or defined the function before the main function then, the compiler has no idea from where these functions came from. But if you declare the function before main then while reading the program it will come across the funky_math function and will know its some details like what is it going to return and what will be its argument types. Or you can define the whole function before the main function , then too it will work fine.

I hope this helps.....

Michael McLaughlin
Michael McLaughlin
14,033 Points

thanks a bunch, both of you guys! does this apply if we're getting functions from different files too?

if we are calling functions from different file then we will be including the header file where the function is stored and header files usually have the function declaration , so when that function is called in the main function then the compiler will have some details of the function as it has already been imported above the main function.

I hope this answer makes sense and helps you in understanding the functions more.