Topic : C++ Tutorial
Author : Unknown
Page : << Previous 3  Next >>
Go to page :


will NEVER get out of this loop, hence the name of an infinite loop, because it will do it an infinite amount of times. Now the only way to get out of something like this is to close the program. Now, lets correct out example so it will work, and NOT be an infinite loop.

int x = 10;
do
{
   x++;
}while(x < 20);


Now, that is better. It will go through the loop, x will be 11, 12, 14, .... 18, 19, 20 then it will break. And hence, no infinite loop. Now, the same thing could be done with the while loop. Here is an example of the while loop in action

int x = 10;
while(x < 20)
{
   x++;
};


Now, when this loop is over with, x will still be 20, just a different way of writing it. Now, the last, but not least loop is a for loop. In my personal experience, I have had more use of for loops and seen more for loops then do-while and while put together. But maybe thats me, I like them more than do-while or while. Lets see an example of a for loop, with the same as our example above:

for(int x = 10;x < 20;x++)
{   //We really dont need to do anything here, the x++ is above   }


And that is it, the declaration is right inside the loop, so is the statement, and the increment is also there. So now you know why I like for loops, smaller and easier in my opinion. Now that I think about it, thats really all there is to loops. Its an easy concept, just takes some practice to master, and eliminate infinite loops. Ive been programming for 3 years, still get the sometimes. But it happens to the best of us. Well, this was short, so I hope that you got everything. Its not too hard, but can be when all of this is thrown at you at once.


C++ Functions and SubRoutines


Now, in most programming languages, you have what is called a function or a subroutine. They are basically blocks of code that are executed when you call the function. This might be a little hard to explain, so lets look at an example, and then we will examine that.

void OutputStuff()
{
   cout<<" Hello World!";
}

int main()
{
   OutputStuff();
   return 0;
}


Now, that may look a little funny right now, but we will look over it and it will make sense by the time that I get done with all of this. Now, like I said above, a function is a block of code that is executed, only when called. Now, lets look at the first function, OutputStuff()

void OutputStuff()
{
   cout<<" Hello World!";
}


Now, the void in this, is a return data type. Remember when we learned about variables ?? Well, int, double and such are data types. And these small little blocks of code, can return values to variables, but more on that later. Basically, void means that we are not returning anything. OutputStuff is the function name. Its just that, its name, like your name, the function has a name. When your mom yells " Bobby ", you answer right ? ( Well, most of us do ), well, when we call the function name, it executes. Now, the actual code is inside of the { }. Only that code will be executed, everything else will be ignored. And that is it. Now, that may seem a little easy right ?? Well, lets get the technicalities out of the way. First off, in order for a function to exsist, you either have to write it before int main or declare it, then write it ( implementation ). Lets look at an example of both.

void FunctionOne()
{
   cout<<" Hello World, yet again !"<<endl;
}

int main()
{
   FunctionOne();
   return 0;
}


Now, that is the first way. I dont like doing it like that, so I will show you the way that I like doing it.

void FunctionOne();

int main()
{
   FunctionOne();
   return 0;
}

void FunctionOne()
{
   cout<<" Hello World, yet again !"<<endl;
}


Now, that may be a little more writing, and take up a little more room. And in a small example like this, it does not really matter. But I find it much easier to do it the second way in larger scale programs. I will be using the second method after this lesson in my other tutorials. Now, again, I used void, so I will show you the reason for having other kinds of data types. Lets look at this:

int GetAge()
{
   int localage; // Local variable
   cout<<" Enter in your age: ";
   cin>>>localage;
   return localage; // Returning whatever was inputted, the value in localage
}

int main()
{
   int age; // Variable to get age
   age = GetAge(); // Assigns to age to what is returned above
   cout<<" You are "<<age<<" years old "<<endl;
   return 0;
}


Now, even though this is brand new, its actually easy once you look at it. localage is a local variable, that means that its a variable that can only be used inside of that function. If you try to use it outside of the function, it will not exsist, and you will get an error. So, when we call the function GetAge() we are basically just executing what is inside of the brakets. So we output " Enter in your age: " and the user will input something, lets say 18. Now, localage now has the value of 18. When the compiler sees return localage; it says " ok, localage is 18, so I will return the value 18 ". And then execution continues at the main function, well, the first thing that happens is that the variable age, is assigned the value of 18. That is what was returned. And then we output it, just like we normally do. Now, isnt that fun ?? Well, thats not all.

Next, we will talk about the fun part of parameters. Yes, parameters. Now, this is not a hard concept to learn, its just a bit funny to new programmers. Remember our conditional chapter ? When we wanted to do things based on certain conditions, well, lets say in our functions, we want things to happen based on certain conditions from other functions. Now, in order to do that, we have to pass in a parameter. Now, you know how we always had the () in our function names ?? Well, that is where we are going to put our parameters. Lets look at an example, then examine it.

bool Legal(int userage)
{
   if(userage >= 21)
      return true;
   else
      return false;
}

int main()
{
   int age;
   bool legalage;
   cout<<" How old are you? ";
   cin>>age;
   legalage = Legal(age);
   if(legalage == true)
      cout<<" Enter the club !! "<<endl;
   else
      cout<<" Go back to Disney !! "<<endl;
   return 0;
}


Now, that is kinda long, but we will go through it, so just bear with me. First off, we have two variables in int main(). They are age, and legalage. age is just simply the users age, and legalage tells us whether or not they are legal. Now, we first off get there age, by cin>>age. Now that we have the user's age, we have to send that information to the function legal, so it can process it. Now, if the user is 21 years of age or older, then we will let them into the " Club ". If not, we tell them to go back to Disney. Now, we pass in the variable age, and what I mean by pass in, is simply this. When we call a function, put a variable in the (). Now, in the actual function itself, we also have a variable. That is just a local variable. In our example, when we pass in age from int main(), the variable userage now has the same value as age. userage can now be used in the function Legal. But that is the only place that it can be used, so we cant use userage in int main() or any other function we have. Now that we have the user's age, we just simply return a bool. legalage now holds true if they are legal, and false if they are not. And back in int main(), we test to see if they are legal, and output the correct statement depending on what legalage is holding. Thats it, now, remember when I told you that I will be using the 2nd method, which is called top-down design. Now, with functions with parameters,

Page : << Previous 3  Next >>