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 Declarations

lets say i have this:

//blahblah
void fancy_func() {
    return;
}
// main blah that uses fancy_func 

Do I have to declare fancy_func in blahblah comment area, even though it's defined before used by main?

1 Answer

Stone Preston
Stone Preston
42,016 Points

if you have a function defined before you use it you dont have to provide a function prototype. If you use it in the file before its defined then you do have to provide a prototype

here is an example of having it defined before use

//function definition
int someFunction(int a, int b) {

    return a + b;

}

int main() {

//function is defined above before we call it, no prototype necessary
int b = someFunction(2, 3);

}

and here is an example where you would need a prototype

//function is defined after we call it, so we need to provide a prototype for the compiler
int someFunction(int a, int b);

int main() {

     //calling the function before the definition, so the prototype above is necessary
    int b = someFunction(2, 3);

}

//function definition
int someFunction(int a, int b) {

    return a + b;

}

Thanks, but that code looks really nice. Where is that hidden IDE in the forum editor, really? I'm sick of just typing with a normal text area.

Stone Preston
Stone Preston
42,016 Points

you have to format your code snippets using markdown. check out the tips for asking questions video on the right of this page. around halfway through they explain how to format code using markdown. its a bit hard to explain in words so seeing someone do it is the easiest way to see how its done

I know markdown well. I'm just not good at tabs and syntax etc etc

Stone Preston
Stone Preston
42,016 Points

ah yeah. Its a bit of a pain typing code in the forum. cant use tab at all just have to use space bar a few times to indent. takes a while to make stuff look nice. but its worth it in the end