hey welcome everybody my name is Chuck McCullough and I'm going to be your host for today's episode
on C plus plus programming today we're going to focus on functions I'm going to give you a quick
introduction to functions in this section and we will continue in future episodes learning all
about C plus plus hey if you haven't subscribed to the channel already hit that subscribe button so
that you're notified when we release new videos we're constantly doing live streams as well as
other produced videos to help you with your Java your c-sharp and your C plus programming skills
without any without any more delay let's Jump Right In
in this section we're going to discuss what functions are the syntax of functions how to call
functions how to declare or prototype functions we'll also discuss parameters and return types
a function is just a named task something that performs some calculation that performs some
task and we give that a name and we encapsulate it in a certain place we've already in previous
episodes calculated the area of a rectangle that is a computation that we might do in
many different places in our application so so that might be something that we want to
encapsulate and just give it a name like get area of rectangle if we provide the function
with the width and height of the rectangle it can return the area of the rectangle
as you can see with this function that's exactly what we're doing let's break that
down though and look at the different pieces of this function declaration first of all every
function must Define a return type what kind of data if any is being returned from the function
the return type is required if your function though doesn't return a piece of information
as a result instead it just performs some action we will specify the return type as void
next in the functions declaration is the name of the function the name is also required
the function's name can be any combination of letters numbers and underscores starting
with a letter and underscore though you can't start with a number in the name of the function
and the name should be a verb or verb phrase and it should describe what the function does
and not how it does it so we would name this function get area of rectangle or compute area
of rectangle or compute area etc those are all good names what we wouldn't want to name this
function is multiply width times height and return the result that describes how
the function performs its task and that's not the goal I know this is very simple but assume
that in a more complicated scenario that other programmers really don't know what the formula
for calculating the area of something is rectangles are easy but there are other
parallelograms and tetrahedrons and all kinds of things that have more complex formulas I don't
want to know how you do it I just want the answer so the name of the function indicates
what we're going to get as a result notice that this function Returns the width times the height
next in the parameters definition is the parameter list after the function name always an open CL and
a closed parenthesis always a set of parentheses even if there are zero parameters for the function
the parameters are declared just like any other variable type followed by the variable's name and
they are listed in a comma separated list there's really no limit to the number of parameters that
can be passed into a function as far as C plus plus is concerned anyway definitely might be
a limit on how much a human could really understand about parameters typically two
or three or four parameters occasionally we'll see functions that have five or six but zero
one two three is much much more common after the parameter list is the function's body the actual
algorithm the task that needs to be performed and that body is enclosed in curly braces
inside the body of that function is just like the code we've already written in our course
we can declare local variables and we can perform any number of statements
if your function has a return type however your function does need to contain a return keyword
so four functions that return a piece of information every code path so if you have
if then else is in stuff every possible exit from the function needs to have a return keyword that
Returns the data that you promised you would return this function re-promises to return a
double and so when calculating width times height double times double gives us a double result
and that's what we're returning from the function
now how do I call functions well we call the function by using the name of the function and
again parentheses passing in the actual values here we're passing in 10 and 20.
notice that syntactically the Declaration of the function the definition of the function
looks differently than the function call when I call the function I don't specify the data
types the parameters and I don't specify that the function has a double return type
so syntactically it's easy for me to tell the difference between calling the function where
actual values are being passed in as opposed to the Declaration of the function or variable
types are being specified I must pass in the right number and the right types of
arguments since the function expects two parameters I must Supply two parameters
the function returns a double so if I want to snag the result and hold it in a variable it must be a
variable of type double as well so the types all have to match doesn't have to be doubled it could
be some other data type like float or int but this particular formula we want double precision
now the function that we're calling must appear before the actual function call so in our code
if I really looked at how this was implemented in main the function get area of rectangle appears
before main the compiler encounters the get area of rectangle function before we call it
then the compiler knows whether we're calling the function correctly or not and can also
knows how to put together the Assembly Language to actually make the function call so the function
has to be visible to the compiler prior to where we're actually calling it or using it now that may
not seem very practical so in a realistic sense what we want to do is do a forward declaration
so instead of implementing the function and the function body prior to where we're calling it
we're going to implement what we call a prototype or forward declaration notice that the function
ends with a semicolon rather than ending with the function body the open and closed curly brace
so what you see at the top is the forward declaration what must appear somewhere else in
our source code and it doesn't have to be the same file but somewhere else in our project would be
the actual implementation of the function and the function where we implement it where we have the
body the curly braces needs to match the forward declaration so oftentimes we copy and paste
anywhere in the in our code where we want to call or use the get area of rectangle function we need
to have this forward declaration and this forward declaration could effectively be duplicated
dozens of times by all of the different Source modules that are using or calling that function
but we need to make sure that we only have exactly one place
in our application where the function is actually defined
so that will be somewhere else and as I said maybe in a separate file in our next session we'll talk
about projects that contain multiple files as well as the purpose of the dot h file the header file
let's summarize real quick the different kinds of of combinations of parameters and return types
for our functions a function many functions do something but they don't produce a result for
the caller to look at the Nike function the just do it function in this case no parameters and no
return that function goes off and performs some task it might prompt the user for information
it may just emit something to the console it might update a database somewhere it performs
some task but there's no result and there's even no input parameters the second case our
function do something same thing no return type no result but it requires some information from
the caller in this case the caller is passing in a string some text and our do something function
does something based on that text but doesn't produce a result to the caller it does return
to the caller eventually but it doesn't produce a result that they can grab and hold in a variable
our last one are calculates something much like our calculate area function as a return type it
does return a piece of information and it does take parameters a string and an INT parameter
in P2 and so we have a bunch of lines of code that maybe does something with the parameter the string
and then ultimately it does need to have a return statement so that it can return an integer result
If I multiply P2 times an integer I get an integer result and the caller can snag that if they wish
hey I hope you enjoyed today's session on functions now go out there and create some
of your own functions maybe take some of those calculations for circles and rectangles from the
last episode and convert those into functions refactor them make them into functions and call
those functions from inside your main code even some of the decision making that we
had in the last episode where we prompted the user for width and height and radius
maybe those could be functions as well I'll let you explore that and next week we will look at
some possible solutions as well as organizing our source code into different source files
with that I hope everybody has a great week if you've got questions or comments or would like
to see anything different leave a comment in the comments below and I'll see you next week