I like @CandiedOrange's answer, but for completeness-sake, there are other ways you can accomplish this, too. You could use a map (sometimes called a hash or dictionary) to map inputs to functions to call. You don't specify a language, but in C++, it would look something like this:
#include <iostream>
#include <map>
typedef void (*actionFunction)();
void functionA()
{
std::cout << "a\n";
}
void functionB()
{
std::cout << "b\n";
}
int main(int argc, char *argv[])
{
std::map<char, actionFunction> functionMap; // <- this is the map of characters to functions
functionMap [ 'a' ] = functionA;
functionMap [ 'b' ] = functionB;
functionMap [ 'a' ](); // <- Calls the function associated with 'a'
return 0;
}
You can do a similar thing in straight C with an array of records that contain the mapping above:
#include <stdio.h>
typedef void (*actionFunction)();
typedef struct functionMap {
char index;
actionFunction func;
} functionMap;
void functionA()
{
printf("a\n");
}
void functionB()
{
printf("b\n");
}
int main()
{
functionMap fMap[] = { // <- here's the map
{ 'a', functionA },
{ 'b', functionB }
};
size_t numMaps = sizeof(fMap) / sizeof(fMap [ 0 ]);
char someInput = 'a';
for (size_t i = 0; i < numMaps; ++i)
{
if (fMap [ i ].index == someInput)
{
fMap [ i ].func(); // <- this calls the function associated with "someInput"
break;
}
}
return 0;
}
These fall into the broader category of table-driven or data-driven programming.