hello this is dr. James camp at Lee College in Baytown Texas and these are my comments on chapter six functions of Toni goddess's starting out with C++ okay 6.1 we start by just introducing the idea of modular programming which is what the the concept of functions fits into okay so far we've been writing programs like this where we we have our int main and then we continue through a long series of statements and the last one down here would say return 0 presumably to indicate that we're ending our program with no errors and that would be the end of the program it just be a long series of statements that are all related basically by the fact that they're part of the same program as you can see that's kind of an eyesore but even more than that it's difficult to figure out what the organization of that block of code is which which statements go together which statements do related things so in this program to the right side of the page the program has been broken down into function to function 3 and function 4 and and really what you should see here in int main is not statement statement statement but function to being called and then function 3 being called and then function for being called and then your return 0 statement okay indicating that that what we do is first we activate function 2 and then when that returns we activate function 3 and then when that returns we activate function four and when that returns we end the program okay this way it's very easy to tell at least if the program was well written that these three statements in function two go together in some logical fashion they form a coherent block of code that does a particular function presumably if this program was well written function three would consist of a group of statements that also do a coherent thing and function four would give us yet a third set of statements that do a coherent thing this is called modular programming it's breaking your program up into smaller manageable functions or modules in C++ we call each of those modules a function okay in any of the C in C or C++ we call those things functions in other languages Java and c-sharp they call them methods but they still do the same basic idea it's a block of code that performs a task and optionally you know I'm going to add this here optionally returns a result okay so functions can work essentially one of three ways you can have a function with no parameters that has no return value just does something ok and because it has no parameters it would do the same thing every time there's no way to tell it to change its its functionality you could have function of parameter does something based on the value of per am okay so this function would do something different every time and then we could have return type F and it could have a parameter or not okay and this one is going to return a value based on program so this one is closest to what you may think of as the definition of a mathematical function you may have returned learned in math that y equals f of X means you input a value X you process it in some way through the function and you output a value for y such that any X will produce a given value of Y that's what functions are named after in C++ is that they can have that same structure where you input a parameter you process that through the function and you return a result okay so how do we define and how do we call a function there are two concepts in C and C++ a function definition and a function call a function definition is a statement that makes up the function itself okay and this will occur once in a program you never redefine a function a function call is a statement that causes the function to execute and this may occur many times okay so in pseudocode we would say define function f of X do X Y and you know two steps x y&z return value okay and that's our definition then later on in our code we can call f of X we can say a equals f of X 1 B equals f of X 2 we could even say C equals f of X 3 times f of X 4 minus f of X 5 we can call it multiple times in the same line of code if we want to okay so those are handy that's a handy snapshot of this is my function definition all of these are function calls okay the definition includes two pieces a function header which provides a return type okay I'm going to look like this return type name per am list okay the paramus always goes in parenthesis the function body then is a series of statements enclosed in curly brackets so that will go after your function header and it will have statement 1 semicolon statement 2 semicolon and optionally there's a return statement that we'll talk about later in the presentation okay so here is a really simple function that we learned way way back on the first day of class that says prints hello world to the screen and then returns a value of zero to the operating system that you know we on this program we don't usually think of it this way but C++ does that the main function is called by the operating system by the program loader as if it were a function of the operating system or something so if I wrote this in a program called hello dot CPP and I compiled it to an executable file called hello and at my command interpreter I type hello well probably I have to type dot slash hello to tell the command interpreter that that's the version of hello I want to run the command interpreter is now going to call main in the executable file hello okay and so that starts out our program right there runs C out hello world runs return zero which passes an error code of zero back to the program loader telleth of the program executed five and exited fine okay now this line int main okay is the function header okay if a function returns a value the type of the return value must be indicated if a function does not return a value its return type is void now note that there are a couple of different words used in programming for things that have no value an object that has no value is called null okay a function that does not return a value is called void so those are different concepts we like to say in English that something is quote null and void but those are actually different concepts in programming note that we have a return that returns the correct type an int for a void we have no return statement okay typically we could put in a we could type return semicolon without returning a value if we wanted to end the function you know explicitly usually the function ends usually a void function ends when it runs out of code when it hits the second curly bracket it just ends but we could end the function explicitly by replacing my no return statement comment with the one word return we'll talk about why we might want to do that later on in the program in the presentation okay function calls we've covered function headers and function definitions now to call a function you use the function name followed by parentheses and of course a semicolon if that's all you're doing on that line when called the program executes the body of the called function whatever's inside the curly brackets for that function after the function terminates execution resumes right after that line of code was called so this function this this simple program here displays a C out hello from main then it calls display message which is going to run up here okay it's going to see out hello from the function display message okay then at the end of that we return to right after the display message call and when you see out back in function main again okay so this shows exactly what I was just talking about the program flow when you hit the display message call program flow moves up to the display message function and when that function is over program flow returns to the next regular and we now return to our regularly scheduled program okay functions can call other functions okay in fact since main is a function itself and it can call multiple functions in the course of its life you already know the functions can call other functions but if you write a custom function it can call other functions either predefined ones like the ones we encountered in C math or other functions that you've defined so you could do int absolute value of a function okay to define the absolute value of a function or turn something okay you have to write your code there and you could do pool is even okay which would return true if the if the number you're looking fat at is even and false if it's odd well you might want to start with the absolute value of so you could say why ABS equals absolute value of y and then you return whatever your code wants to say okay so when your main program down here calls if is even of 22 okay we're going to take 22 pass it up to is even and then is even is going to immediately pass that value on to absolute value of x okay and then that is going to return a value here and this is going to return a value here okay so functions can call functions and those functions can call functions etc before the compiler can call a function it needs to know all the things that are defined in the function header it needs to know the name of the function the return type of the function how many parameters and the datatype of each parameter what I haven't been specifying here in my my lazy example programming is that this should have said aunt absolute value of int X because we've got to have a data type for our parameters okay so if the compiler has to know the function header before the function can be called how do you notify the compiler about the function before your program ever makes a call to the function well there are two ways probably the more conventional way is to simply place the full function definition before the calling functions definition okay but sometimes you have functions that call each other okay in a back and forth way if you have f of X and G of Y and this called this but then this called this back you can't have both of them fully defined before you get to the other one okay one of them has to be defined first so a way to get around this is to use function prototypes the full function definition looks like this void print heading parenthesis open curly bracket whatever your code needs to do close curly bracket there's no semicolon at the end notice that okay the prototype looks like this void print heading parenthesis semicolon okay this tells the program that you're reserving room in memory for that function a function called print heading but that you're not defining it yet you're going to define it some time later you still have to define it sometime later in the program or you'll get a compiler error but this lets you set aside that that name ahead of time okay so the person who writes this Gatiss likes this idea of declaring your main function in writing at first and then writing all of your other functions after it so which is not the style I grew up learning but he defines void first and void second as prototypes here notice that they both end with semicolons and then on the next page of code he writes your void first including the open curly what does it do close curly then he writes void second including open curly what does it do close curly know that these are void functions so they don't have a return statement they just do what they do and then the function ends um some bits of prototype style it's best to put prototypes near the top of your program that makes sure that the program knows all the functions that it's going to be defining later and then whatever order you want to define them in later the compilers already been notified you still must define the function but you can now place the function definitions in any order and when I mean any order I can have a set of prototypes ant f of X semicolon sorry I keep forgetting my and F of ant x semicolon double G double y semicolon and then I can define G double G double Y open curly do my stuff I can define G first then I can define main and then I can define double F sorry I can't define double F because I defined it as an int earlier and F of int X open curly do my stuff close curly so I can define these in any function any order in the file that I want as long as the prototypes have been made public at the very beginning of the code here one way or the other okay whether you choose a full definition or just a prototype the program must define the function header before any calls to the function otherwise you'll get a compiler later so you can choose either the style that I learned growing up which is define all the function at the very beginning of your file or you can choose goddesses style put the prototypes at the top of the program and then define your functions in whatever order you want through the rest of the program either is is good C++ style okay sending data in function this is called passing values okay you can pass the values into a function at the time of a call okay we've already seen an example of this with the power function that we learned for turning a to the B into that okay we're you know these are arguments to the function pal okay POW is defined somewhere in the C math library and it probably isn't defined with a and B it's probably defined with you know base and exponent or something like that wherever it's defined you know it might be defined as POW base double base double exponent the value that's passed as argument a is passed into parameter base okay and the value that's passed as argument B is passed into parameter AXA okay and then the function can do whatever it does with those to calculate base to the exponent power okay a function with a parameter variable once something is passed into this parameter that parameter can be used like a normal variable anywhere else in the function okay so if I want to display a value and so I write a function called display value and it takes a parameter num I can write C out the value is and then past num as if it were a variable that I had declared in my function okay this is an example of that same idea we take display value 5 we pipe it up into this parameter int and what it does this aunt is called num it comes down here to this the value is num and so we get this line output the value is 5 we took the the 5 that was passed into num and we display it there okay the parameter in the function header is also sometimes called the formal parameter or some people just to be confusing call it the formal argument okay the argument in the function call is sometimes called the actual parameter or actual argument okay so you have the formal one which is how it's defined in the function and the actual one which is what your the value that you're actually passing the prototype must include the datatype of each parameter inside its parentheses the header must include a declaration for each parameter in its parentheses and the call must include a value of the correct type for each parameter in its parentheses so in order for these to all match up even or odd prototype only has to say int we don't have to give it a name yet we can usually we do but we don't have to the header which would be followed by open curly do some stuff close curly ok by the function definition that has to have not just a type but an actual name okay and then when we call even or odd down here in the in the program okay we have to pass a value in there now does that have to be a variable no okay we could call even or odd three so that would be kind of silly to know because when we're writing the program we know that 3 is odd so why would we want to do that but we could we could call even or odd X plus 3 so you can actually have an expression as long as it evaluates to a number of the right type okay you can have an expression inside the parentheses okay the value of the argument is copied into the parameter when the function is called okay you don't change the actual value down in the calling function you make a copy of it a parameter is in scope only within the function that uses it okay so that int num into scope when the function is called goes out of scope as soon as the function ends so you can't reference num back in the calling function a function can have as many parameters as it needs I've seen functions with the ten parameter lists you know there's nothing wrong with that except that it becomes cumbersome to write them there must be a datatype listed in the prototype and an argument declaration and the function header for each parameter okay and arguments will be promoted or demoted as necessary to match the parameter so if I pass a double to a function that expects an int it will get truncated to just the integer part getting passed to the function if I pass an int to a function that expects a double which is what happens with although the C math library functions then the int will be automatically promoted to a double and the math will continue okay when passing multiple arguments the number and order of arguments in the call must match the prototype in definition so if I say int F of double X character C do some stuff end and then down in my code here I want to call F a 3.14 no okay this would not call that function because the the headers do not match okay I have to switch the order here and make the 3.14 come first and the a comes second now that will be a valid call and this first the first argument will be copied into the first parameter the second argument will be copied into the second parameter and so on so if I did have double X comma double y I have to specify X and y in the correct order okay passing multiple arguments here's an example of one that takes int int + int and adds them up I think this is a lousy example because it doesn't actually matter what order you put these in but the point is we call show some with value 1 in orange value 2 in green value 3 in blue and so value 1 gets copied to num1 by u2 gets copied to num2 value 3 gets copied to number 3 and then they get added and the sum is displayed now in this case it doesn't actually matter what order you put them in because addition doesn't care about the added that the but if you had - and times here then it would definitely matter which order you passed the numbers in okay this is to note that so far what we've been doing is called passing by value that means when an argument is passed to a function its value is copied into the parameter okay so if I call f of 3.14 and I'm doing ant F of double X 3.14 is copied into X where this becomes important is if I have another value down here called a variable down here called XYZ and I I'm going to copy the value of XY Z into F and anything F does with this value does not copy back to XYZ okay so I could I could write x + equals 3 here that's gonna change X but it's not going to change the original XYZ it's not going to make any other the copy doesn't work backwards okay so we can use functions in menu-driven programs in a couple of interesting ways one is we could make each user choice from the menu a function so we could say you know if choice equals go then we call go else if choice equals stop call stop else dot dot okay so each each one of our menu items could be a function another way we can use them in menu driven functions is to implement something that every choice is going to do so if choice equals one I forgot like double equals here lazy programmer if choice double equals one do option one here report result else if choice equals two to option two here report result else dot dot so you see that in each case we need to report our result and so rather than writing that code multiple multiple times and having to change it you know if we decide we want to change how we report the results we'd have to go to n every else if every if and else if and this whole program and and change the reporting code instead we can write one function up here void report result that does exactly what we needed to do and then we only have to if we make a change in how we want to report our results we only have to change that code once up here okay now I told you we'd come back to the return statement here we are the return statement is used to end the execution of a function or return from the function okay it's usually the last statement in a function but it can be placed anywhere in a function okay and it becomes the last statement in the function because everything that follows return will not be executed this is usually done if there's some sort of an error condition that's detected and so you need to return early from the function or you know perhaps there's some function that something that makes it very easy to return a simple answer from that function instead of having to go through all of the functions code so for example if I had an averaging function that took a list of numbers and returned to the average if it detected that only one number had been returned it had been passed to the function there's no need to add up the numbers and divide by something you just return that one number right so real simple we could do if count equals one return number one okay and then else we do the process of adding up all the numbers and dividing by count in a void function without a return statement okay has to be a void function because otherwise you need a return statement the function ends at its last curly bracket the function just runs until it runs out of code and then it returns automatically okay so imagine that we're passing two numbers to a function called divide this is one of those where there's an error code that you need to return so if the second argument is zero then you would other what you would be dividing by zero you can't divide by zero so we see out an error message and we return early from the function otherwise we see out the correct answer and return normally okay usually however when we use the return keyword it's because we want to return a value so a function can return a value back to the statement that called the function you've already seen this with all the C math functions that we studied the POW function takes two arguments a base and an exponent it does it's you know whatever calculations it does internally and then it returns the result of them to be passed into in this case the the variable X in a value returning function okay a function that returns a value the return statement can be used to return a value from the function to the point where it was called okay so here's we define a variable of type double called result result gets to be num1 plus num2 take our two parameters and add them okay now why did we make result a double that's a really good question I don't actually know the answer here this should be an int to match the end here the end here and the end here some compilers would even give you a warning about that indicating that that these numbers are all going to be promoted to doubles and then demoted right back to an int but then we take whatever value has been calculated in return in result and we return it to the calling function now notice this is this is good programming style to declare your variable on one line do your calculation on another line do your return on another line however if you're pressed for time or space you could consult you could condense this entire line to say return num1 plus num2 okay you don't actually have to declare a result variable and store the result in it and then return that you could just return the sum okay and so you'll sometimes see programmers who are just wanting to save programming space and typing keystrokes and all of that put the entire function definition on one line here return num1 plus num2 okay see even shows you how to do that here okay okay so this shows you we're taking total is sum of value 1 in green value 2 in yellow so value one gets passed here value 2 gets passed here and then the sum is returned back up to the calling program I hate that he breaks this across two pages because I can't show you that ah but he's got a version of it here okay so return gets passed up to total note that I can also use function results returned results in C out statements so I could have just put C out the total is some value one value 2 okay so that's a valid this is valid C++ to just sticking the the result of a function right into an output statement the problem with that is you you do your calculation and you lose the value immediately it gets put out to the output but the program never knows what the output is this is usually better style here because you store the value somewhere and you can use it again later if you need to okay another example area equals pi times square of radius radius gets copied into number number gets used twice in this case now to get number times number and then the product if number times number is returned and fed into this equation so it's multiplied by pi and the product is stuffed into area okay returning a value from a function the prototype and the definition must indicate the data type of the return value it cannot return void okay if it's got a void value you can't return a value the calling function should use the return value in some way either assign it to a variable or send it to C out or use it an expression you can have multiple function calls that return values on the same line you can say ant y equals f of X 1 minus G of X 2 times H of X 3 okay so you can you can include multiple function results we would pass the number into X 1 pass a number into X 2 pass a number and X 3 get the results passed out and those results would be multiplied and subtracted and etc and the whole function the whole expression be evaluated the normal way okay it is sometimes useful to return a boolean value okay if you define a function with the prototype rule f of say int X okay then the function can return one of two use true or false okay the function body must contain a return statement that returns a value of true or false okay note that true is equivalent to 1 and false is equivalent to 0 so you could have an integer calculation that returns either a 1 or a 0 but typically when you write a bool function you want it to return the literal value true the literal value false or the result of an expression that does the same thing so you could say return X is less than Y and this will either be true if X is less than Y or false if it's not okay so is even would apparently return true if the value is even false if it's not and we can see that that's here if the number % 2 equals 0 status is true else status is false and then we return our status variable we could even do that with that one line C++ return num percent to equal 0 question mark true if it is not a semicolon a true : false if it is not so we can put that whole thing on one line again C includes a lot of these shortcuts that let you throw an entire set of program logic into one line that does not mean that is always the best style for C++ this is arguably better style because it's easier for a casual programmer reading through the code exactly what's going on but but this is totally legit C++ does exactly the same thing and you know saves you a few times of hitting the return key okay local and global variables variables defined inside a function are called local meaning just like we say you know food grown in the same city that it's sold as local food variables defined inside a function or local that means however they're hidden from statements and other functions okay and because variables defined in one function are hidden from variables and other functions you can often have multiple functions with the same name so there's a num here and there's a numb to find here there are two different variables that point to two different memory locations and they're each private to the function that they're in so if we see out num here and see out num here we get different results depending on whether we're in main and another function or back in main okay the functions local variables exist only while the function is executing okay so this is known as the lifetime of a local variable when the function begins its local variables and its parameter variables are created in memory they're actually pushed on to something called the call stack and when that function ends those things are popped off the stack and they cease to exist anymore that means that any value stored in a local variable is lost between calls to the function unless you make it a static variable which we'll talk about a little bit later a global variable is a variable defined outside the call stack it's defined before any other code in the program is executed so it's outside the scope of everything so it's in scope for everybody okay the scope of the global variable starts when it's define at the top of the program and goes for the rent you know includes all the other functions in the program this is bad style okay using a lot of global variables makes your program difficult to debug it's very easy to define a global variable int X and then later down here in main to define another int X and it's not clear to the reader which X is being used it's clear to the compiler this one's more in scope than this one so this one gets used and that one does not is that what you meant maybe maybe not okay so that's a side of using global variables when a lot of people use Global's for our constants okay so in a business program like this one here your pay rate your base hours your overtime multiplier those are constants that are defined presumably by the person who pays the programmer salary the programmer doesn't get to choose those so they're defined at the beginning of the program and they're used you know they're available for any functions inside that program to use this is something we'll do a lot of in engineering programming that if we have certain parameters of our system that we're simulating you know we have a spring constant we have certain things that are true constants like the acceleration due to gravity or the ideal gas constant or things like that we can define all those constants up in a a global constant block at the beginning of our program usually right after all of our includes and usings okay and before we start actual programming okay and that means these constants are our true constants they're going to be the same anywhere else in our program and they'll be available to all of our functions however you've got to be careful not to redefine those somewhere else and come down here and you know int men has base pay don't if somebody had defined that as pay rate that would override the constant and that would cause all sorts of headache right okay so this shows how you can use those global constants if our is greater than base hours base pay is base hours times pay rate over time is ours - base hours pay rate times ot multiplier okay local variables are not automatically initialized okay inside a function so inside int main for example if I just declare ant X X is not initialized and that means that int will hold whatever it means that X will hold whatever value was already in memory at the location that was chosen to put X n so it's gonna be a random bit of crap really okay global variables on the other hand if I define up here ahead of nth a head of int main if I define nth Y this is auto in it to zero okay does the program because these are defined at the beginning of the program there is nothing in that memory location prior to defining that so these are defined with empty memory locations okay you can have local variables that are static this is a unique concept for C++ as far as I know local variables only exist while the function is executing when the function terminates the contents of those local variables are usually lost we talked about that they get popped on the stack they get put on the call stack so we get parameter X as long as the program is executing it exists then it's removed as soon as the function is ends being called static local variables retain their constants between phone retain their contents between function calls they're not allocated on the call stack they're allocated kind of with the global variables so they're only initialized the first time the function is executed zero is the default value you can initialize them your own way if you want in this case so we see that int local num is not defined as a static so when it's changed that value is lost as soon as the program as soon as show local disappears so the first time show local is called it's set to 5 the second time local show local is called it's still 5 it's set to 5 again rather okay if we define it as a static int okay then each time the program is called we're going to plus plus it so it's zero the first time 1 the second time to the third time and so forth and so on so if you want something that only exists inside a function but persists from one call to the next to the next you can make it static okay you can also initialize a static to something other than zero just by putting in equals after it however this is only initialized the very first time the function is run after that any changes you make to it are accumulated from function called a function call I've actually never used that functionality but it's cool to know that it's there okay default arguments when you define a function sometimes you don't want to specify all the parameters okay in that case you can make some of them you can give some of them default arguments so if you want to say have a sum of a variable number of numbers you can set the default argument for each of them everything but the first to zero and then it adds up as many as you as many numbers as you pass in okay if I pass in a number here to say and then I pass in a number three say that will override the default and set that to three but I don't have to pass something to this next one and so get some will just be two plus three plus zero okay so I could write a get some with up to three up to ten arguments all of which are default zero except the very first one and then no matter how many I you know I can pass between one and ten and anything I don't pass will be automatically set to zero so this one both of these have default arguments so this is a function that will display this many stars in one row and this mini you know green number stars in a row yellow number Rose so by default it's going to one two three four five six seven eight nine ten but you can override that so this one we just first time we just called the default we should get ten stars in a row this time we call with one parameter so that overrides ten we should get five stars in a row the third time we override both default parameters we should get a seven by three grid and you see that's that's what we get we get ten the first time five the second time and seven by three the third time if default arguments don't make sense to you you don't have to use them there's there's never a situation in C++ where that's the only solution to a problem but they can make your life easier if certain situations arise that it would be useful to have a default argument more importantly you need to understand that other functions that you may be called calling yourself functions that you may be using from somebody else's library someone else may have written them with default arguments and this is true of a lot of the things in the C++ standard library they've been written so that you can just pass a single parameter but if you want you can specify other options by passing additional parameters note that if you're going to have default arguments they have to start at one point and continue for the rest of the function you can't have a default argument and then not have one here that this is a no-go okay similarly when you start calling arguments you can pass one to the first one to the second and nothing to the third you can't pass one to the first nothing to the second and one to the third okay that this situation is another No so C++ will look for something to fill the first spot something to fill the second spot if it doesn't see anything in the third spot it will assume that you want to go with the default argument okay using reference variables as parameters we've talked about passing by value before you can also pass by reference and that means that you refer to the original variable and that allows the function to modify the values in the calling environment okay it's usually used as a way for a function to return more than one value because C++ functions can only have one return so if I'm trying to get the dimensions of a room or something like that I need a height and a width I can't have just a single return value so I pass a value by reference for width and a value by reference for length and it returns me the values note that when you do this you have to call it as get dimensions length width okay I have to specify these must be variables I can't pass numbers I can't pass expressions I have to pass variables because it's the it's a link to this variable that's being passed and it's a two-way link so that if I make a change here that change is echoed back down here okay the way you do this is by putting an ampersand between the type and the parameter okay it's conventional to put a space between the type name and the ampersand but that's not important you must use the ampersand in both the prototype and the header okay so if I want get dimensions I can say double ampersand double ampersand semicolon then later on when I define the functions get down I have to say double I still have to put the ampersand in with double ampersand length okay use when both necessary and appropriate okay don't use when the argument should not be changed by the function okay don't use this when you're just passing values to inform the function but you don't want changes to come back don't use if the function only needs to return one value it's better C++ style to say X equals f of X then to say f of X ampersand yeah it's better to to take a pass by pass by value and do a return than to do a pass by ref makes you function more generically usable too because in this system you could then later see the need to say X equals f of X plus one and and that's that's legit if you had done this as F of double ampersand X that function call wouldn't work because that's not a single variable that you're passing okay one of my favorite topics overloading functions this is something that not all programming languages can do but C and C++ definitely can overloaded functions have the same name but different parameter lists okay a good example of this is if you want to average some numbers okay I've done this in a program before average of X comma Y is really easy to calculate you return X plus y over 2.0 okay average of X Y Z is easy to calculate return X plus y plus Z over 3.0 okay those cost me almost nothing to write and they cost the PERT the computer almost nothing to run there's no loops necessary nothing fancy involved but if I get average of a list okay then I have to do 4x equals 0 X less than list size X plus plus some plus equals list at X and some div equals list size okay or or a return this is better style return sum divided by this size okay so I've now created three functions called average the compiler will determine which version of the function to call based on the argument and parameter lists that I pass okay so if I call average of 2 comma 4 okay it's going to pass to 2 X 4 to Y and run this version of my thing if in the next line I say average 3 6 9 it's going to see that I have three parameters it's going to pass them to XY and Z and choose to run this version of my function ok so if you had things that have different sets of dimensions you could create overloaded functions that had different number types different number and types of arguments and parameters and the compiler would choose automatically based on which one is the best match okay another way is if you happen to know is if it happens to be better to do math within a certain type here there's one square function for int and one square function for double and they both return number times number but one of them is doing integer math and one of them is doing floating-point in half and so it actually does make a difference the integer math runs through your computer slightly faster than the floating point math so letting the program choose to do integer math if you're only worried about integers then that will ever so slightly speed up your program okay there's an exit function that terminates the execution of a program I'm not sure why we're throwing that in right here but okay it can pass an into the operating system to indicate the status of program termination this is essentially the same as return with a number in your int main except that this can be called from any function so if you have any kind of error that happens in your program a divided by zero or something like that that you have to just terminate the program you can pass a value to the operating system to indicate that you you had an error and you can terminate the program right there it does require the C standard Lib header file so you've got to pound include that if you're going to use it okay exit zero is the exit with no error there's almost never a reason to use that because if your program is exiting normally it should be exiting at the end of the program not some somewhere randomly elsewhere that's that's another thing that makes your program hard to debug and shouldn't really be used but you can define several failure codes or maybe your operating system is already defined exit you know failure codes for you so you can exit with the predefined failure code 4/0 error and it will tell the user what happened okay last bit here if you're testing code and you haven't written the full code yet there are things called stubs and drivers that you can use to test your code a stub is a dummy function used in place of an actual function usually declares it you know displays a message indicating it was called so if you're in the middle of writing a program and you know that average of a list needs to be done but you're just not ready to write the code to average that list yet you can see out we would be averaging here and return zero because presumably this would be a function that returns the average so you just return a dummy value zero is gonna set everything in the program to zero so maybe we return one just to have a useful value but that's a stub okay a driver is a function that tests another function by calling it without necessarily having anything useful for it to do so if you have written your average function you want to make sure it works you can write a driver that says call averages and you have a series of C out statements that average of one two three is average of one two three okay and you keep doing that for as many cases as you want to test and then you get to see when you run your program exactly how average is working for you okay wow that was a long presentation sorry to have taken so much of your time but functions are an important topic and we need to cover them properly okay as usual if you think you learned something leave me a like and give me your comments in the section below and next time we'll be up here talking about arrays and vectors in C++ how to deal with more than one value at a time