We saw in the earlier chapter about the function main() as pre-requisite for any C program. To understand C a bit more, let us call (invoke) functions from main() and understand what it means. Later, we will use this concept to understand functions in detail.
void myFunction1(void); /* line 1 – this is declaring functions before use*/
void myFunction2(void); /* line 2 – also called as declaring the prototype */
void myFunction3(void); /* line 3 */
void myFunction4(void); /* line 4 */
printf (“Hello, I am printing main()\n”); /*line 5 of code */
myFunction1(); /* call or invoke function as they are declared already */
void myFunction1(void) /* this is called function definition. Contents of the method */
printf(“Hi, I am in myFunction1\n”);
printf(“Hey, I am in myFunction2\n”);
myFunction3(); /* calling function from another function. Just like main() calling other functions */
printf(“Hurrah, I am in myFunction3\n”); /* printing a function name within quotes is a text */
printf(“Howdy, I am in myFunction4\n”);
If you save the above file and compile and run it, you will get the following output:
Hello, I am printing main()
Hi, I am in myFunction1
Hey, I am in myFunction2
Hurrah, I am in myFunction3
Howdy, I am in myFunction4
Pretty straightforward, isn’t it? Let us see certain key concepts of programming here:
1. Declare the prototype first before using it. If you call a function without declaring its prototype, compiler will give error.
2. Void – here indicates there is nothing passed or returned (let us deal with this later)
3. Definition of the function/body is required mandatorily. If you declare a function but did not define it, compiler will give error.
4. \n within quotes – is instructing the compiler to insert a new line after execution of the statement.
5. Comments in each line are created by /* */. When a compiler sees this /*, it skips compiling the text until it sees another */ and it is meant for the developer to understand the program better at a later point of time.
If you see the code from main(), it calls 3 functions one by one. Hence that will be the order of execution. And another point to note is that main() calls myFunction1, myFunction2 and myFunction4 only. myFunction3 is getting called from myFunction2. So you can see that you can call functions in your own order.
I suggest you to explore the above a bit more by changing the text, calling functions from a different function and calling functions multiple times. Let us C more in subsequent chapters.