- C program, like any program when done professionally,
can very quickly grow out of hand.
This means they can become very large in size
and spread over multiple files.
This is why we need to be able to use several tools
that the C language gives us
in order to make our code more manageable.
One such tool, it's actually a process or a best practice,
called Breaking Up Code Into Blocks.
It's much easier to debug, to plan,
to write, to review and improve
if the code is not all written in one big chunk.
This is why we have a mechanism in C,
as we do have in other languages, called functions.
Those are blocks of code designed to perform a task.
There are two main categories of functions in C,
ones that are built in, like getchar and printf,
that are predetermined and predefined,
and they're part of the standard libraries,
and the others are completely at the discretion
of the programmers.
They can have any name you want.
However, we'd like to note one notation,
called a Camelback Notation,
especially useful when you have very large words.
calculateSumOfTwoNumbers.
Notice that every word that makes up the function name
starts with a capital letter.
This is a function that does
just what we would expect it to do,
calculate the sum of two numbers; the numbers are a and b.
Let's see how this is done in code.
This is an example of a fairly large C program,
definitely larger than the ones you've looked at,
so far, in the videos in this course.
A quick scroll towards the bottom,
you will see that there is a main program in Line 21.
There are task 1, 2 and 3 above it.
There are codes to multiple tasks.
There's even a code to specialTasks, and so on.
You can see how this can get out of hand very easily.
Sometimes it's even hard to see where the programs start.
We'd like to reinforce the fact
that every C program requires a main function,
and only one; you cannot have two mains.
There has to be exactly one,
and this is where execution starts.
Some of the functions, like specialTask,
is actually included in a separate file
via the include specialTasks in Line 14.
Let's quickly see what is in those files.
As we saw earlier, it is possible to define
multiple functions external to a current program,
using the include command, to have access to them.
In this case, there are more interesting things
that can be done with functions.
For example, in Line 34, task_1 actually doesn't do anything
except call in task_3.
task_3, it's a specialTask, which its name hints
is defined in the specialTask library.
If you look at specialTask_3,
it says specialTask_3 was called.
The reason we did that is that we wanted to have
unique functionality so, for example,
if I want to make some changes just to specialTask_3,
I only focus on Lines 24 through 30
in the library specialTask.
This is the beauty of breaking your code into blocks.
Let's run and see what happens.
When I run this code, you see that
all the things that were in main and in various tasks
were executed up to the point
where I'm being asked a number.
Let's quickly look at the main
and see where would this have come from.
See how !!!Hello World!!! was printed.
The multiple task was called,
and some of those functions is calling task_3,
which is entering a number.
Let's enter a number, and now specialTask_2 was executed
because, after task_3, in main,
specialTask_2 is being called.
So, using this approach, we were able to create
a fairly complex web of links
in which external libraries
played an integral role in running this code.