Author : Unknown
Page : << Previous 4 Next >>
when you declare them, all you have to do is just put the data type in there. Like this:
bool Legal(int); // Decleration for function
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;
}
bool Legal(int userage)
{
if(userage >= 21)
return true;
else
return false;
}
Thats it, just put the data type in the decleration. Now, it will not hurt to put the variable name in there also, but that it totally up to you.
Now, we have different kinds of parameters. The kind that I just got done introducing was called value parameters. What I am going to teach next are called Reference Parameter. Now, I showed you above that you can pass in a variable in a function, and then you can treat it just as a normal variable. Now, if we pass in a reference parameter, then we can change the variables value in different functions. Lets see an example then examine it:
void ChangeVariable(int &x)
{
x = 20;
}
int main()
{
int varone;
varone = 30;
cout<<" variable one: "<<varone<<endl;
ChangeVariable(varone);
cout<<" variable one AFTER CALL TO FUNCTION: "<<varone<<endl;
return 0;
}
Now, after we declare a variable called varone, we set it equal to 20, and output it, which is 20. Easy enough, now, when we call the function ChangeVariable(), it has a reference parameter in the parameter list. Now, just like any normal variable, we can use it like a local variable. But when we change the variable x anywhere in that function, it also changes the variable varone from our main(). It will not make much sense on why until some chapters on down the road. But, I will just say this. When we use the & operator ( called the address of operator ), we are getting the address of the variable in memory, and basically, we are assigning the variable to the same address, so they will maintain the same value. This will come again when we are referring to pointers, but for now, just know how to use it and what it does.
When we go to use top-down design ( declare the function, write the function later ), this is how we do it with a reference parameter, lets take our example from above.
void ChangeVariable(int &);
int main()
{
int varone;
varone = 30;
cout<<" variable one: "<<varone<<endl;
ChangeVariable(varone);
cout<<" variable one AFTER CALL TO FUNCTION: "<<varone<<endl;
return 0;
}
void ChangeVariable(int &x)
{
x = 20;
}
The next, and last concept of the function chapter, are global variables. You know how we have had variables declared inside of functions, now we can have variables used in EVERY function in your program, without having to pass parameters. And all we do to do that is just declare them outside of the main function. For instance, lets look at an example:
int age; // Global Variable
int main()
{
cout<<" Enter Age: ";
cin>>age;
OutputAge();
return 0;
}
OutputAge()
{
cout<<" Your Age: "<<age<<endl;
}
Now, you see how I am using the same variable in two different functions ?? That means that it is a global variable and can be used wherever I need it in my program. Now, some people dont believe in global variables unless you NEED THEM. I dont agree, I believe use them as you see fit, but dont go over board. I generally go by the rule that if I am going to use the same variable in more than two functions, then I use a global variable. Anything else is local.
Now, with that, I end the functions chapter. Functions are a concept that must be learned in any kind of programming to be a successful programmer. If you dont understand functions, then ask and ask and ask until you do understand them. They are a MUST.
Hello, and welcome again to another fun-filled chapter of C++. Now, we have covered the basics of C++, now we can get into some more funner things, like this chapter. ARRAYS !!. Now, Arrays are not that hard of a subject, but they do sometimes give people a hassle. First off, lets define an array
Array is basically just a data type that is one variable, with many different values. Now, that may seem confusing, but lets think of an example. Lets say that you had to write a program to calculate the average test scores of your class. Now, I for one would not like declaring 30 variables and trying to find the average of them. That is where an array comes in handy. With just declaring one variable, I can put 30 different values in it, and use it with ease. Lets look at this example:
int TestScores[30]; // Declaring array to hold 30 test scores
Now, when we declared this array, the acutal variable name is called TestScores. This variable has a data type of int, and it can hold 30 different integers. Thats what the TestScores[30] means. Now that we have this array, we have to fill it. We can access each element in the array by using a [] operator, called the indexing operator. That may seem confusing, but lets look at this small example.
int Numbers[3]; // Will hold 3 different integers
Numbers[0] = 13; // This is setting the first element to 13
Numbers[1] = 18; // This is setting the second element to 18
Numbers[2] = 21; // This is setting the third element to 21
That is how we assign different values to the array. Now, what may seem confusing at first is that we declared the array to go up to 3, but we only used an index of 2. Index is what number is inside of the box ( [] ). That is because we are including zero in the array, so instead of having the index range from ( 1 to 3 ), it ranges from ( 0 to 2 ). In our example, if I tried to use Numbers[3], I would have some bad things happening to me. So the range of an array that is declared with N integers are ( 0 to N-1 ). Now, if we have 30 integers, how are we going to set all 30 integers ?? Well, I am having some memory recolections to chapter 2 or 3, where we learned about loops :)
Lets say that we wanted the user to input the test scores, and if we had 30 of them, that would be stupid to do cin>>TestScores[0]; ............ all the way up to TestScores[29]. So lets use a for loop to get the input, lets look at this example.
int TestScores[30];
for(int i = 0;i < 29;i++)
{
cout<<" Input a Number: ";
cin>>TestScores[i];
}
Now, thats ALOT easier than trying to go through and cout'ing and cin'ing 30 different times. Now, after that loop is done, we now have an array that is holding 30 different test scores. Now, lets say that we want to output all of those values, we do the same thing. Just alter it a tad.
int TestScores[30];
for(int i = 0;i < 29;i++)
{
cout<<TestScores[i]<<endl;
}
Now, we have 30 different scores that were just inputed and we are still holding them. Now, with an array we can do wonders with larger amounts of data. We can do a whole bunch of statistical analysis on it. But for right now, we will just find the average of the test scores :) Lets look at the code, then analyze it:
// Lets assume with the code above, TestScores already has all 30 scores
double avg;
int total;
for(int i = 0;i < 29;i++)
total = total + TestScores[i]; // total += TestScores[i] is the EXACT same thing
avg = double(total / 30);
cout<<" The Average is: "<<avg<<endl;
Well, now aint that fun ? Lets look at it. First we had to declare two additional variables, one, to get the total of all 30 scores, and the variable to hold the average value in. Notice I used a double for the average variable, that is so we can have a decimal in the answer. That is just for a more exact answer. The for loop should look familiar, I am looping through to achieve all 30 values. And the entire time, we are accumulating the total in total. After the loop, that line may look funny. total / 30 should look ok, but if you notice, avg is a double, and total and 30 are both integers. Now, what I am doing is called Type-Casting. Basically, I am taking
Page : << Previous 4 Next >>