8.0 Reusing Code
The concept of reusing code seems a bit obvious, but it’s the most often ignored concept. If you ever do something right, its best to set it up to be easily used over and over again. Lets say you want to write a line of dashes across the screen whenever you want. You could just write out something like this:
for (x=0;x<80;x++)
cout << ‘-‘;
but this is a bit redundant if you have to do this over and over again. What if you could just call a library or something to do the same thing? Well..you can make one up yourself. Consider the following program:
#include <iostream.h>
void printdash()
{
for( int x=0; x<80; x++ )
cout << ‘-‘;
cout << “\n”;
}
int main()
{
printdash();
cout << “menu option 1)\n”;
cout << “menu otpion 2)\n”;
cout << “menu option 3)\n”;
printdash();
return 0;
}
Whenever you needed to print out the line, you just called the function called “printdash” and it worked. Now all functions require a return value, and in this case the return value is “void” or basically nothing. It could be used to return an integer, a float or whatever. Consider the function to square the value of a number:
double sqare( double value )
{
return a*a;
}
the key word “return” is used to send the value back as a number, and can be even used as a number value.
Q = Q * square(24);
(In fact...if you’ve noticed...most “main()” functions also return a number. This is a hold over to the original use of C, namly UNIX made by Bell Labs. This is to tell the operating system one thing or another about how the program did. In most cases, returning zero works just fine).
8.1 Reusing Arrays in Functions
Now what if you need to pass an array? Remember the whole point of arrays is that the variable itself is a pointer to the first element of the array. So if you wanted to pass an array of characters, when it gets to the function, the array becomes a pointer of characters.
int printValue( char* value )
{
cout << value;
return 1;
}
int main()
{
char name[200];
strcpy( name, “Philip Milkenstien Wiskerstern” );
printValue( name );
return 0;
}
8.2 Reusing Code with Changing Variables
Now what if you wanted to use a function to change values in the functions. Remember the whole idea that variables are pointers to addresses that contain the actual values. This works the same way with functions; you can pass variables “by value” or “by address.” When you pass information “by value” then only the information gets passed in. No matter what happens inside the function, nothing in the variable will be changed. However, when you pass by address, then the actual address of the variable gets passed in, and everything you do to the variable will change its contents. This is often referred to as “side effects,” mainly because it is considered bad practice to rely upon side effects to change the value of a variable. However, there are always situations where this is a good idea. In most cases, relying upon the returned value is more appropreate.
The following is an example of something called a “bubble sort”, the most basic and common way to sort values.
#include <iostream.h>
void swapit( int& a, int& b )
{
int temp=a;
a = b;
b = temp;
}
main()
{
int array[10] = { 1, 35, 53, 22, 55, 66, 4, 42, 44, 65 };
int n=10;
int x,y;
for (x=0; x<n; x++)
cout << " Index #" << x << ") " << array[x] << "\n";
for(x=0; x<n; x++)
{
for(y=0; y<n-1; y++)
{
if(array[y]>array[y+1])
{
swapit( array[y], array[y+1] );
}
}
}
cout << "\n\n";
for (x=0; x<n; x++)
cout << " Index #" << x << ") " << array[x] << "\n";
}
The passed in function holds the address, and allows the values to be changed and used in the function.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment