Wednesday, April 9, 2008

Make your code Smaller with Function Pointers and Templates

While programing we find ourselves with things we would need to write over and over again. Even tho copy paste is very friendly to use, it tends to make the code larger, and since we are bound to make bugs every now and then. Its not easy fixing hundreds of times in the same error, so most of us use functions. Functions are an essential part of programing when it comes to extremely complex algorithms, but sometimes functions aren't enough and we still end up writing the same processes over and over again. For example we might have a common process, lets say we have a simple loop (of course a simple loop is simple but for the sake of explaining lets use a loop) so lets say we have something like this
void primary_process(int beginning, int times)
{
int x;
for (x=0;x<times;x++)
cout << beginning + x << " ";
}
yet as we keep on programing we notice that we use that loop a lot of times, the only thing that changes is the formula that we use on the number. So we could write yet another function with the same loop.
void primary_process_multiply(int beginning, int times)
{
int x;
for (x=0;x<times;x++)
cout << beginning * x << " ";
}
But that would be writing the same loop function once again. What we can do is use a function
int sum(int a, int b)
{
return a+b;
}
int multiply(int a, int b)
{
return a*b;
}

once again this functions are extremely simple but in a real programing environment functions could take hundreds of lines. Now that we have this two functions lets use function pointers.
a function pointer can be declared like this
class (*function_name)(classes)
so if we whant to rewrite our loop function but using a function pointer we do this
void primary_process(int beginning, int times,int (*function)(int,int))
{
int x;
for (x=0;x<times;x++)
cout << function(beginning,x) << " ";
}
Now you must notice how I added a function pointer in the parameters the function will take, and then I used the function pointer like if it were a normal function. How we use this?
well here is an example
primary_process(1,15,sum);
cout << endl;
primary_process(3,14,multiply);
by writing the name of the function in the third parameter, I pass the function I want to use to the primary_process function
now its important to note that the function pointer must contain the same amount of parameters as the function it will point at otherwise it will fail to work.
Now we have made our code smaller and without duplicates. But what if another problem arises. Lets say we want to play those same processes but to other types, in our example we want to use characters too and also output characters. Well we could copy all 3 functions and make functions that get characters in them OR. Use templates.
look at this code.

template <class T>
T sum(T a, T b)
{
return a+b;
}
template <class T>
T multiply(T a, T b)
{
return a*b;
}
template <class T>
void primary_process(T beginning, int times, T (*process)(T,T))
{
int x;
for (x=0;x<times;x++)
cout << process(begining,(T)x) << " ";
}
by using templates in functions we allow it to take any type of class, it could be an integer, a character, even a custom class. of course there could be exceptions but I won't discuss them today. Yet this way allows us to use different types in similar processes now for using this functions I ran this example code.
primary_process('a',5,sum);
cout << endl;
primary_process('b',10,multiply);
cout << endl;
primary_process(1,15,sum);
cout << endl;
primary_process(3,14,multiply);
this returned.
a b c d e
b ─ & ê Ω L « ► r
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 3 6 9 12 15 18 21 24 27 30 33 36 39

In a larger programing environment we might use classes that need to go trough the same processes, and this processes might take hundreds even thousands of lines, and sometimes having duplicates of those processes will cause enormous headaches when debugging.
By using this methods we can have cleaner codes, and be able to change them fast in the future instead of going trough a all the copies of itself.

back to the code.

0 comments: