Coding C++ with generic functions


What is a generic function?


Download Code Example Here

A 'generic' in C++ terms is similar to an int, double, or String,  - except it means 'any type'. 
They provide a way to write code that will work with any data type - even those that haven't been defined yet. This means you can write a function that doesn't know in advance what data type you'll be putting into it so you can give it ints, doubles, or even strings. It's up to you. 

The way you declare this generic type is with the following syntax:
template <class anyClass>

So, a full function might look like this:

template <class anyClass> void swapVals(anyClass &a,anyClass &b){
  anyClass temp;
  temp = a;
  a=b;
  b=temp;
}


In this example, you can pass any 2 variables of the same type, and no matter what type they are, their values will be swapped. 

For instance, if you add this code to 

template <class anyClass> void swapVals(anyClass &a,anyClass &b){
  anyClass temp;
  temp = a;
  a=b;
  b=temp;
}

void setup() {
  // put your setup code here, to run once:
  int a = 5;
  int b = 6;
  Serial.begin(9600);
  Serial.print("a=");
  Serial.println(a);
  Serial.print("b=");
  Serial.println(b);
  // prints 
  //a=5
  //b=6
  swapVals(a,b);// swaps a and b
  Serial.begin(9600);
  Serial.print("a=");
  Serial.println(a);
  Serial.print("b=");
  Serial.println(b);
   // prints 
  //a=6
  //b=5

 
}



Why would I do this?

Well, what if you needed to do similar things with both integers and double values? You'd have to have 2 different functions with different names, so you'd need to write something like this

void swapInt(int &a,int &b){
  int temp;
  temp = a;
  a=b;
  b=temp;
}
void swapDouble(double &a,double &b){
  double temp;
  temp = a;
  a=b;
  b=temp;
}


Using generic functions, we can use the exact same code for either of  those values so you only need 1 function for any datatype. 
For instance, we can pass in doubles, or even strings to our function, and it acts exactly as expected.
So, you could do something like this, for instance:
 
void setup() {
  // put your setup code here, to run once:
  int a = 5;
  int b = 6;
  double da = 5.0;
  double db = 6.0;
  String sa = "hey";
  String sb = "there";
  
  Serial.begin(9600);
  Serial.print("a=");
  Serial.println(a);
  Serial.print("b=");
  Serial.println(b);
  Serial.print("da=");
  Serial.println(da);
  Serial.print("db=");
  Serial.println(db);
  Serial.print("sa=");
  Serial.println(sa);
  Serial.print("sb=");
  Serial.println(sb);
  // prints 
  //a=5
  //b=6
  //da=5.0
  //db=6.0
  //sa="hey"
  //sb="there"
  swapVals(a,b);// swaps a and b
  swapVals(da,db);// swaps da and db
  swapVals(sa,sb);// swaps sa and sb
 
  Serial.print("a=");
  Serial.println(a);
  Serial.print("b=");
  Serial.println(b);
  Serial.print("da=");
  Serial.println(da);
  Serial.print("db=");
  Serial.println(db);
  Serial.print("sa=");
  Serial.println(sa);
  Serial.print("sb=");
  Serial.println(sb);
  // prints 
  //a=6
  //b=5
  //da=6.0
  //db=5.0
  //sa="there"
  //sb="hey"

}








Generic functions might not always be a good fit for your project,  but they can provide a nice way to minimize your code and make it easier to make changes in the future.
As always,
Happy Coding!
~Tom



Comments

Popular posts from this blog

Using GIT with Arduino

Programming Arduino with Regular Expressions

Organize your Arduino code with header and class files