- We have been using in most of our examples
prototypes of a function.
So, now it's a good time to pause
and discuss this for a few more minutes.
So, what is a prototype?
As a reminder, it allows for a function
to be used before it is defined.
Especially when we have multiple functions
and we don't know exactly in advance
which one may be needed before another.
In order to avoid any problems with errors,
we are simply declaring before the main a prototype.
A prototype literally means,
I am advance declaring the function,
but here is a little bit of a difference.
The function itself, if you look at the bottom,
it says, "double func, double x, and double y."
The prototype doesn't have to include
the variable names, just their types.
In a way, you can refer to it as a template.
So, we are now creating a template of a function,
that returns a double and requires two double arguments.
In main we are going to declare
yet another double result, which will capture
whatever that function is supposed to do
once it receives those two values; 10 and 20.
And it returns in this case the product.
Notice, it's a minutiae here that
even though the function requires two doubles
in this example we are sending two integers,
We already know that C,
it's not very picky when it comes to this.
As a reminder, as I said in other videos,
C assumes you know what you're doing,
it will not always alert you.
This is why we will see shortly,
in another portion of this course, how do we ensure
that we don't make such inadvertent mistakes.
For now let's go to the code, and here is our function,
the prototype; double func(double, double);
and the request for a result from two variables; x and y,
that are being multiplied inside the function.
So, let's see what happens.
All we have left to do is click Run,
and the result is 200, because ten times 20 is 200.
And what the function did it took those 10 and 20,
converted them to double, multiplied them,
sent the result back as double,
so now it is 200.00000.
The key point in here that I wanted to show
is the fact that you declare a prototype of the function
as a template, that doesn't require anything
except the type of the data, that the function returns,
and the type of parameters, if any.