What is the Difference Between return 0 and exit(0) in a C++ Program

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Paul in this video I'm going to be explaining the difference between using the return keyword versus the exit keyword when leaving AC or C++ main function so normally what we see at the end of our main functions is we see this return 0 what this is indicating is it's saying if we get to this point we're going to exit the main program and we're going to return this value back to our system so if I go ahead and save this file here and then I go ahead and compile the file in my terminal actually I should show you here that I've actually got that file in this current directory here and you can also see it here so this is two ways I'm looking at the file here if I go ahead and I'll clear the screen and I'll come tile the leave main CPP file which is what we're looking at here and we'll give it an executable name of LM for leave main by including this - oh flag and naming the executable LM like this go ahead and push enter now we've created this executable so we can run the executable by typing dot /lm and as you can see nothing happens because we didn't really instruct our program to do anything interesting but something actually did happen we actually did return a value and we can see this value by going back to our terminal here and typing in echo dollar question mark and we can see that return value here so to demonstrate this a little bit better let's just go ahead and change this value to a five and we'll save it we'll go ahead and compile it once again and then we'll do dot /lm to run the program and now if I say echo dollar question mark we can see that we now have the return value of five so another way that we can exit the main program is we can use the exit keyword and so the exit keyword has parentheses and then you put the return value within the parentheses so let's just go ahead and use three this time we'll save that clear the screen and then we'll compile one more time we'll run our executable and now we'll go ahead and say echo dollar question mark to see what our return value is and we see we have the value three so what is the difference between using exit versus using return well there's one key difference and in order to see that let's a little bit of coding within the main function so we can choose one or the other and kind of see the results so let's go ahead and get rid of this for now and we're going to create a string variable as you can see I've included the string library here and so I'm going to create a string and I'm just going to name this string choice and it's basically going to hold the choice that we want we are going to either want to exit using the exit keyword or the return keyword we're going to store one or the other of those words within this choice variable and so then we'll just do a do-while loop and so do means we want this to happen at least one time we'll say C out to prompt the user to enter some information and let me go ahead and expand this a little bit more here and we're just going to ask the user to choose exit or return so we'll say exit or return and we'll end that with a semicolon and then we'll grab their input from the keyboard and we'll store it in our string variable that we named choice and so the do is saying do this at least once and then we'll make sure that they entered either exit or return and not some other strings value so we'll accomplish that by saying while choice dot compare exit and choice stop compare return semicolon so what in the world did I just do there so okay the dude says do this one time and then the while says continue to do this as long as this guy and this guy is true okay so true in boolean means that we have a nonzero value so essentially we're saying if either this call returns zero or this call returns zero then this will be false and we won't continue to do the stuff inside of do anymore so when are we going to get a 0 here or a 0 here well this string dot compare in our case what the user entered we're comparing that with the word exit and if the user entered the word exit this will return a zero if that's the case this wild condition will get a zero or false and we will no longer go back into this new section likewise if what the user entered we compare that value with return if the user entered the word return this guy right here will return a zero and then the while we'll look at that and say oh this is false which means it won't go back into the do section again and it will exit so basically what's happening here is we ask the user for input we grab their input and we're gonna keep asking them for input until they enter either exit or return so that's what happens here so one thing to point out is I can do this because I'm using the string class here and so choice is a string and compare is a method that's part of the string class and so I'm kind of using just some information that's included in this library here to take care of this in an easy way now that the user has entered either exit or return we're going to check to see what they entered so once again we'll look at the choice that they entered and we'll compare that to the word exit and if what they entered is the same as the word exit then this compare method is going to return the value 0 so if that's the case for now we'll just print out you chose and then we'll just print out what they chose just so you guys can see what's happening here we'll put a new line and let's go ahead and give myself some more space here and so if they didn't choose the word exit and that means they chose the word return so we'll just go ahead and print that out for the moment so we can make sure this is working the way we think it is and I suppose we should put a space here in the space there so things look nice and since they chose exit here let's just go and say exit 1 for example we could do that and maybe for the return we'll put return 7 let's go ahead and save this will clear our terminal we'll compile the program name the executable LMK and then we'll run the program and so we get prompted exit or return let's just type in something different than that just to make sure that our do-while loop is working correctly and so we'll just say leave and it says exit or return because it didn't get one of those values so ok let's say exit this time and it says you chose exit and exit our return value was 1 so let's go ahead and see if that's what we got back by saying echo dollar question mark and we got the value 1 clear the screen one more time and I'm just going to give us a space here we'll save the program recompile you run the program and this time I'll type in the word return and it says you chose return we'll type echo dollar question mark to make sure that we got the value 7 and we did so let's go ahead and clear the screen now so the do-while loop seems to be working the way we expected it to when we choose exit we exit with one when we choose return we return 7 so so far these are looking really similar so what is the difference here so to see the difference what we have to do is let's just go back to the top of the program here and let's create a class so I'll call this class and I'll call it leave and we have to end our classes with a semicolon and we'll create a public section in here so inside of our public section we're just going to create a constructor and AD constructor so that we can see the difference between these two keywords so to create a constructor we simply type the name of the class open and close parentheses and then we can go ahead and define that what the constructor does here and so this is what's going to be called whenever we create a new leave object so all we're gonna make our constructor do is we're going to make it print the message object has been created but a new line there and in that with a semicolon and then we'll create a Deconstructor by typing tilde the name of the class in this case it's leave open and close parenthesis and then inside of the body of the d constructor we're going to print a message that says objet and destroyed okay so I saved the file here so basically the constructor is going to be called when the object is created and the D constructor is going to be called when the object goes out of scope and in the D constructor you'd want to make sure that you are the allocating memory as necessary so that when you're done using the object you don't leave yourself with memory leaks in our case we're just going to print these two messages to let us know when the object is created and when the object is destroyed so now let's go down to our main function and let's go ahead and create a leave object so I can do that by typing leave the name of the class and then we'll name our leave object and I'll just name it l for leave so here we have a leave object named L let's go ahead and save our program and we'll run it once again and see what happened so first we want to compile and we'll name our executable with a - oh flag we'll name it LM once again and then we'll go ahead and run our executable by typing in dot /lm and so here we see that the object has been created so let's go ahead and choose to exit our main program by typing the keyword return so we'll type return and it says you chose return and your object has been destroyed we can echo the result and we can see that it's seven as we would expect from our return so if we exit on a return keyword our object gets destroyed so let's go ahead and clear the terminal and we'll run the program one more time the object has been created this time let's exit the program using the exit keyword in this case when we use the exit key word the Deconstructor did not get called when we exited the main program and that's the main key difference between using the exit or return keywords so what does this actually mean to you as a programmer if you're using a normal operating system the operating system is going to deallocate this memory for you upon exit so this is really only an issue if you're not using a standard operating system the main difference between the two is when we use the return keyword the Deconstructor of our objects are called whereas if we just use the exit key word we just simply exit the program without calling any B constructors for our objects so anyway that's the difference between using the exit and return keywords thank you guys for watching and make sure you subscribe to stay updated on new releases from my channel thank you guys for watching have an excellent day and don't forget to subscribe
Info
Channel: Paul Programming
Views: 38,569
Rating: 4.8307691 out of 5
Keywords: C++, return 0, exit, keyword, return, return value, string, class, string class, What is the difference between exit and return, paul, programming, paulprogramming, c++ return 0, exit vs return, return vs exit, what, is, the, difference, between, tutorial, computer science, sublime text, compare, string.compare, do, while, loop, do while loop, Computer Program (Literature Subject), Programming Language (Software Genre)
Id: j3LL3lhkCTU
Channel Id: undefined
Length: 9min 54sec (594 seconds)
Published: Sat Oct 31 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.