Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Pointers and Memory Structs

Humza Choudry
Humza Choudry
237 Points

Before main() and after main()...?

In cases like these as well as your experiences in programming, what is the purpose of declaring a function as such before int main() oppose to after main()? would it make a difference? In what cases do you use it after and before?

2 Answers

Denis Kiselev
Denis Kiselev
12,737 Points

In C before you can use any function, you need to inform compiler about it. When you use standart functions (like printf) you use include statement to include header file with declaration of some standard functions (like stdio.h for printf). Indeed, stdio.h contains a lot of function declaration for standard input and output.

So, you need to declare function before use of it. Declaring a function is writing what return value it have, its name and its arguments. So, when you will use your function in some part of program, compiler can check if you use correct arguments and if return value type matches.

Implementing function can be done anywhere in program: before main.c, after main.c or in separate file (you can add myfuction.c to program and place implementation of yours functions there). After compiling whole project, compiler will use special compilation stage named "linking" to inter-connect functions from different modules of program. That's why when you only declare function, but forget to implement it, you will recieve error from linker: at first stage compiler will only check calling function for parameters match and return value match, and it will use declaration for checking, not implementation. After that, linker will try to find impementation to connect it with caller entity. If linker fails, it will raise an error.

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

It is called prototyping, to use your function in main() {} you have to tell your compiler where to grab the code from to execute it. You have to put the prototypes above the main and then you can define them after main (it is only for convenience - to make your code look cleaner and more readable); Now when the compiler will run into your function inside main() it will think: "aha! I remember this was mentioned before!" Then it goes to check your prototype (up there, before main()) and from there (judging by the signature of the function) it is routed to the bottom where the actual function is declared and it takes the code from there and replaces the function inside your main() with that code.

I hope that helps