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) 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