- [Voiceover] Now that we have covered the subject
of functions a little bit,
mathematical expressions,
and a few other topics,
we are ready to integrate all of them
and create even more interesting and useful things in C.
On the topic of functional programming,
it's worth recalling some of the things
we mentioned earlier.
Functions are blocks of code
that perform specific tasks.
For example,
"calculate a mathematical expression,
"evaluate the outcome of a series of statements,
"or print results in a specific format."
Some of the advantages are obvious.
"Enables reusability of portions of code,
"enables breaking complex solutions into simpler steps,"
and "simplifies the debugging process."
And finally, it "eases the process of code update
"and improvement."
Let's see all of this to work right now
in an actual program.
Let's imagine the following scenario:
A program that assesses the number of empty seats
in each one of three rooms
in a building, in a museum,
or whatever your context may be.
You then ask the user to enter the numbers
of waiting visitors for each room.
It assesses each room, its still vacant seats,
the overall capacity,
and the average number of visitors
waiting per room.
Let's look at the four functions
that are defined for this program.
One is "assessRoomCapacities,"
the other is "estimateWaitingVisitors,"
"processData," and "generateReport."
Those are the four functions that
it's very obvious, or at least
legible and plausible what those function may do
by just reading their names.
That's another feature of C.
You can use names that make sense.
It enables you to write functions with those names,
and then if you look at the "main,"
the "main" is very short,
and it simply lists the steps
that you are about to perform when this program runs.
You simply assess the room capacity,
you estimate the visitors, you process data,
and you generate some report.
In order to do that we define
in line 27 - 29,
multiple global variables
that are known throughout the program.
Like,
Room 1, 2, and 3, which will tell us
how many seats are empty in each room.
Visitors 1, 2, and 3: how many visitors are waiting.
Then we'll be able to calculate
total available seats and total waiting visitors.
Now let's look at
each one of those functions.
As you can see already,
I was able to think about this problem
without worrying about 'how will I implement
'each one of them,' I simply created a skeleton
for the main program.
I typed the names of all the functions
that represent functionalities
that I want this program to do.
All I have to do now is start implementing them
one by one.
For example,
the function called "assessRoomCapacities"
simply ask the users
for each one of the room,
to enter the number of vacant seats.
The second function,
that estimates the number of waiting visitors,
asks the user to enter for each room
how many visitors are still waiting.
Finally,
there is a function to process data,
and the generate the report.
The function to "processData" does two things.
One, it adds the total number
of seats available in each room,
and then the total number of waiting visitors
for further processing.
The "generateReport" then combines a few things
you've already seen in a previous video.
First, it assesses for each room
the number of visitors
compared to the number of seats available,
and uses a boolean logic operator.
If "room1" is bigger than "visitor1" for example,
the result would be "true," or "1."
And so on for each room.
It also calculates the total available seats
in comparison to the total number of waiting visitors,
and also displays a 1 or a 0
based on whether there are available seats.
Finally,
it calculates the average number of visitors per room,
by simply adding all the visitors
and dividing that by 3.
Let's see what happens when we run this program.
First we're going to enter the number of seats left
in room 1, let's say "30."
Number of seats left in room 2, let's say "20,"
room 3, let's say "10."
Visitors waiting for room 1: 12.
Waiting for room 2: 5.
For room 10: 9.
Here's our result.
"Overall there are still seats available," yes or no.
1 means "yes," there's still seats available.
Room 1 can accept for visitors? Yes.
So is room 2, and so is room 3.
"On average,
there are 8.67 visitors waiting per room."
As you can see,
this program allowed us
to take a task, breaking into smaller, simpler ones,
develop them individually,
and now we can easily update
each one of those functions
without much interaction
and affect on the others, as needed.
This is the beauty of functional programming.