In this video, we'll learn how to define our own functions.
Function definitions have four main parts.
We'll look at the syntax of functions in the next slide.
But for now, let's look at what these parts are.
The function has to have a name.
The name of the function follows the same rules we
use for naming variables.
Function names should be meaningful and provide the reader
the intent of the function.
For example, a function called func1 and another function
called func2 may be okay when teaching functions in my slides.
But what if I read some production code with function names like that.
I'd have no idea what these functions are doing based on those names.
Functions perform operations.
So usually, you see function names as verb or verb phrases.
This isn't always the case, but it's a good habit to get into.
The second part of a function is the parameter list.
This is the list of variables that the function expects
from you when you call it.
For example, if you're calling a function that adds 2 integers,
then the 2 integers are in the parameter list.
Sometimes functions don't need any parameters at all.
Suppose I have a function that simply displays hello or a function
that returns a system time to me.
These functions can be called with an empty parameter list.
All functions have the ability to return information to
the caller of the function.
In order to return information, we must specify the type
of that information.
So I might return a Boolean or an integer or a double or a string.
Also it's possible that a function returns nothing.
For example, if I need a function to just display hello,
I really don't expect anything back from that function.
In this case, we say that the return type is the keyword void.
Finally, functions have bodies.
These are statements to be executed and they're enclosed in curly braces.
You've already seen an example of this in the main function.
So as you can see, a function definition is pretty logical, the
name of the function, what it expects, what it returns and what it does.
Let's see how this looks in c++ syntax.
Here, we have a simple function definition.
Notice the name of the function is function underscore name.
And after the function name, we have open and closed parentheses.
In this case, this function expects no parameters.
So there's nothing inside the parentheses.
This function returns an integer.
We specify that by providing the type of the return value
before the function name.
Finally, we have the body of the function, which consists of
statements inside curly braces.
You can see that this is the same syntax as we've
used in our main function.
Of course, we can have functions that return nothing expect
many parameters and so forth.
The syntax for all of them is very regular and exactly
what you would expect.
Let's see another one.
In this case, we have the same function named function name.
But this time it expects exactly one parameter.
That parameter is an integer type and it's available for us to
use in the body of the function.
In this example, we have a function that returns nothing when called.
In this case, we provide the keyword void before the function name.
This tells the compiler that there is no return type.
With void functions, we can still have simple return statements in the body
of the function, but they're totally optional and not often written.
If there is no return statement, it's understood that the function
returns after the last statement is executed, pretty simple syntax.
Let's look at one more.
In this example, we see a function definition for function name
that expects multiple parameters.
In this case, the function expects two parameters: an
integer and a standard string.
When the function is called, these two parameters must be provided
as arguments in the same order as specified in the parameter list.
Suppose we're going to write a function that displays hello
followed by an endline every time it's called, we could easily
define that function as follows.
The function name is say hello.
It expects no parameters and returns nothing in.
The body of the function, we have an output statement
that just displays hello.
Notice that in this case, there is no return statement required since
the function doesn't return anything.
Once the say hello function is defined, we can call it from
main or from any other function.
In this example, you see the function say hello defined above
and you also see that it's being called by the main function below.
This program will display hello.
In this example, we're calling the say hello function 10 times inside
a for loop in the main function.
This will display hello 10 times once per line.
Of course, we can define as many functions as we need
in our applications to help modularize our program.
In this case, I'm defining 3 functions: say world,
say hello and main.
Execution always begins with the main function.
So in this case, main calls say hello.
So we execute say hello.
And we display hello to the console.
But then say hello calls say world and we display world.
So this program displays hello world to the console.
Here's the same example with some other output statements sprinkled in.
Again, execution always begins with the main function.
So in this case, main calls say hello.
So we execute say hello.
Remember that when say hello is finished, we need to
complete the code in main.
So we display hello to the console then say hello calls say world.
And when say world is done, we complete the code and say hello.
So we execute say world and it displays world to the console,
followed by bye from say world then we go back to where we left off and say
hello and execute bye from say hello.
Then we go back to where we left off in main and execute bye from
main and our program terminates.
You can walk through this code and you'll see exactly what's happening.
There is one caveat about function definitions.
The compiler must know the function details that is the function
name, the parameters and so forth before the function is called.
So look at this simple example.
I'm calling say hello in main.
But the compiler hasn't seen the definition of say hello yet.
In this case, you'll get a compiler error telling you that say hello
has not been declared in this scope.
We'll talk about how to solve this issue with function
prototypes in the next video.
But before we do that, let's head over to the IDE and write a few functions.
Okay, so I'm in the IDE, I'm in the section 11 workspace in the
function definitions project.
What I want to do here is I want to write a program that calculates the
area of a circle on the volume of a cylinder, pretty easy calculations.
But I want to break everything up into functions that we define, we
create our own user-defined functions.
And this is what I want my main to look like.
Two simple function calls: area of the circle, volume of the cylinder.
That's it.
Now within those functions, we can prompt the user to enter the radius,
the height, whatever else we need.
Okay, so let's get started.
Let's write that area circle function first.
Now in this case, I don't want to pass anything into the function or
return anything from the function.
This function is simply going to ask the user to cut to
enter the radius of the circle.
It's going to call another function that will actually calculate the
area, and it'll print the result.
Okay, so let's write that function.
We'll start with void since it returns nothing.
And the name of the function is area of the circle.
That's it.
Simple as that, nothing in the parameter list right now because
we're not passing anything in, as you can see here in the call.
Okay, so what do we need?
Well, we need to ask the user for the radius of the circle, right.
So we need a variable here.
And we'll just call it radius, and double is perfect.
We'll initialize it to 0, and we'll say, we'll prompt the user and
put a new line at the beginning.
We'll just say enter the radius of the circle.
That's it.
And we'll read the input into the variable we just created.
You can see this pattern happening over and over
again, it's pretty standard.
Okay, now what do I want to do.
Well, now I want to call another function that
actually calculates the area.
That function -- again, remember, the area for a circle is pi r squared.
That function needs to know the radius in order to calculate the area.
Let's write the function call first.
We'll say something like cout, and we'll do something like
again the area of a circle with radius whatever they typed in,
which was radius in this case.
And then we'll say is, now what do we do.
Now we need to call a function that actually calculates that
area gives me back the double that I can display here.
Okay, so let's call that function calc area circle.
And that function expects the radius, right.
And then we'll print a new line at the end.
And really that's it for this function.
Let me clean that space up right here, right.
So now we need to write the other function, this function right
here, calc area of the circle.
Obviously, we need to return something from that function, right.
The function's calculating the area.
So it needs to return something to me so that I can display it here.
So in this case, I want to return a double.
And let's call it calc area of the circle.
And what does it expect, it expects the radius, which is a double.
What does it do now, it just returns pi r squared, right, simple as that.
But we haven't defined pi.
Let's define it up here.
We'll say const double pi
and we'll set it to 3.14159,
that's close enough for this example.
So there's my pi.
It's constant, which is what we want.
We don't want to be changing pi.
And in the function body here, all we need to do really is to say
return pi times the radius squared.
We could use the pow function or we could just say radius times radius,
which is what I'll do right here.
That's it. That should do it.
Let's walk through this.
We call area of the circle
transfers control over to this function.
We have a radius variable right here, we're asking the user to enter
the radius of the circle, we're reading it and then we're passing
that value into this function, calculate the area of the circle.
And it's going to calculate the area of the circle and return
that value back to me, which I'm going to print right in here.
Now I could store that into a variable, do anything
I want with it, right.
So let's run this and test it out, oops.
Let me comment .volume of the cylinder because we haven't written that one
yet, that's the next one, right.
So the radius of the circle 12.5.
It says here the area of a circle with radius 12.5 is 490.873.
Okay, obviously, we'd want to test that to make sure that that's correct.
And that's pretty much it, right.
Pretty straightforward.
Now let's write this one right here.
The volume of a cylinder.
Well, how do you calculate the volume of a cylinder.
Well, the volume of a cylinder is pi r squared times the
height of the cylinder, right.
And I'm not using any units here.
You can think of this as inches or centimeters or meters or
whatever you like, it doesn't really matter too much.
So we need to write this volume cylinder function.
This is going to be very similar to the area circle function, right.
It's just going to prompt the user for some information.
In this case, it's going to prompt the user for the radius of the cylinder
and the height of the cylinder.
So let's write it.
We'll say void volume of a cylinder.
And the reason that that's popping up in the completion is because I had
that in there before and I guess it remembered, which is kind of handy.
All right, so we need the radius of the cylinder.
And we'll initialize that to 0.
And we also need the height of the cylinder.
We'll initialize that to 0 as well.
All right, so let's ask the user.
Let me just copy and paste this to save a little typing time.
So what is the radius of the cylinder in this case.
And we'll read that into radius.
Perfect. And then what we'll do here is we'll copy these guys as well,
and we'll ask them for the height of the cylinder,
enter the height of the cylinder.
We'll read that into the height variable.
All right, so now we've got everything we need.
So what do we do now.
Well, let's do the output statement, right.
We'll say cout, and we'll say the volume of a cylinder with radius, and
we'll provide the radius value here.
And then we'll say something like and height and we'll
output the height here.
And finally, we'll just say just like before is, now we
need to call a function which we haven't written yet, right.
So let's call it calc volume of the cylinder.
We'll write that function in a minute.
Now what do we pass into it.
Well, we need to pass in the radius and the height.
It doesn't matter what order you put it in, but that's the way you
have to provide the variables.
So I could have said height radius and just sent in the correct ones.
So in this case, I'm going to write that so it expects a radius
first and then the height, and then we'll print a new line
at the end, and that'll be it.
Okay, so that's where we're at with this function.
It's finished now, so let me clean up some of this space right here.
Now what we need to do is write that function, calc volume cylinder.
So let's do that.
And we'll do it right here.
We'll say -- actually we'll do it a couple lines up right under the
other calculation, right here.
So we'll say double because it returns a double, calc
volume over the cylinder.
It expects the radius, which is a double.
And it expects the height, which is also a double, right.
So in this case, what do we do?
We return pi radius squared times the height.
And that gives us the volume of the cylinder.
That should do it.
Calling volume cylinder now, so we'll do it 2 times now, we'll get the
area of the circle, which let's say it's -- again, let's just say 10.
And that's correct.
That's what we expect, right.
Pi times 100.
So what's the radius of the cylinder this time?
Well, let's say it's 12.1.
And what's the height?
Let's say it's a really tall cylinder, 25.2.
So the volume of the cylinder with radius 12.1 and height 25.2 is
11.591, and we just need to put a little space after the is there.
Okay, now there's one thing that we could do to make this a little better.
You'll notice that this calculation right here pi r squared, we're
doing it again right here.
So what we could do is we could refactor this code
just so it's a little better.
And we could say return and we could do we can calculate the area of the
circle and pass in the radius to it and then multiply that by the height.
That way we're reusing that function.
Okay, so hopefully, you can see what's going on here.
The volume of a cylinder is the area's -- the area of
the circle times its height.
So we may as well just call that function to do it for us.
You can see the repeated code.
Okay, so there you go.
That's it.
We've created -- and let me run that again just to be sure that we're
working correctly, see 12.1, so what's the radius of the cylinder, 10.2.
And let's say it's 30.1., and there we go.
Okay, so that's it.
You can see how really straightforward it is.
And the nice thing about this is when you read your
code, you can see your main.
It's really easy to follow, right.
Before in our main, we had all of this stuff in there.
Now we're just seeing area of the circle, oh okay.
And remember, I can call these as many times as I want.
In this case, it's going to ask me two times.
I could put them in loops.
I can do anything I want.
So what's the radius of a circle?
10.1.
Okay, it's asking me again as I called it again, 10.2 this time.
Then it goes to the radius of the cylinder.
Okay, so that's it.
That finishes off this video.
You can see how easy it is to create these user-defined functions.
And this is really what you want.
You want your program to be modularized and have these small,
well-defined units that do what you'd want it to do and just do
one thing really, really well.