- [Instructor] A function is an atomic standalone
block of code.
It may or may not have parameters were returned value
C++ makes no distinction between a function and a procedure.
This is func.cpp from chapter two of the exercise files
a function definition includes a return type,
the name of the function,
and a pair of parentheses
and a block of code enclosed in curly braces.
It may optionally specify parameters inside
of the parentheses.
This function has no parameters
and it has a void return type.
The void return type means it does not return a value.
A function is called by using its name and parentheses.
So when I build and run this,
you notice that the puts in main is displayed first.
And then the function is called,
which displays the puts from inside the function.
So we have the result that says, this is main.
And then this is func.
I may add parameters to the function.
I can come up here and I can say int I.
And so this declares a type integer parameter
with the name I, and so now inside the scope
of the function that variable I is now available.
So I can change this puts to a print F
and say this is func
and inside the parentheses I'll put I,
and then I'll put I as a parameter to print F.
So print F this is actually a function call.
It has two parameters.
The first parameter is this string
with the replacement placeholders.
And the second parameter is the I
which will be used by print F to display the value.
And so now when I call func
you notice it says no matching function called to func
because part of the function signature is the parameter.
So now there is no function called func with no parameters.
There's only a function called func
with one integer parameter.
And so if I come in here and say 47
and I build and run this
now you see it's calling func with the value 47.
It passes that 47 to the function as integer I
and then it goes and passes that to the print F for call.
Of course, if I like I can add more parameters
I can say const char asterisk S
and so now it also wants a string
and I'll put that in the print F like this.
And now if I try to build this,
you'll notice that I get that error,
no matching function call to func
because we're looking for a function called func
with one integer parameter,
and it's not finding that.
So that's part of the function signature is the number
and types of the parameters.
So now I can say string.
And when I build and run
you'll notice that my result includes that string.
A function may also return a value
and so going to give this a return value of int.
And now, if I try to build this
you notice that I get an error that
control reaches end of non void function.
In other words, this function is expected to return a value.
And so I can return say, I times two.
And now I can say int X equals my function call
and the variable X will now contain the result.
And so I can say print F X is percent D X
like that and now when I build and run,
you'll notice we get that 94 result which is 47 times two.
Functions are a vital part of C++ programming.
And we'll cover them in a lot more detail
later in this course.