String as a function name ??

could someone suggest an alternate piece of code for this

void setup() {
Serial.begin(9600);
}

void loop()
{
String c="loopk";
c();
}
void loopk()
{
Serial.print("strings can be used as function names");
}

("strings can be used as function names");

No they can't.
Tell us what you want to do, not how you think it should be done.

i simply want to use a branch statement

C doesn't have a branch statement.
You're going to have to tell us more.

its k .im using a mircorpocessor for the first time. just trying out simple programmes.. i was used to using branches in assembly language.. i think i can manage without. thanks anyway

preejit:
its k .im using a mircorpocessor for the first time. just trying out simple programmes.. i was used to using branches in assembly language.. i think i can manage without. thanks anyway

String c="loopk";
  c();

You can't do that in assembler, but whatever.

AWOL:
C doesn't have a branch statement.

Does it have a goto statement?

Yes. But we don't talk about it in poilte circles.

Agreed. I have been programming for ... longer than I care to remember. You don't need branch or goto statements in modern code. It makes things incredibly obscure. If we taught you how to use them, next you will be complaining that your program doesn't work.

AWOL:
Yes. But we don't talk about it in poilte circles.

And if you talk about it in impolite circles, you get beat up.

jraskell:
And if you talk about it in impolite circles, you get beat up.

I think goto's have their place. I've implemented plenty of FSAs as goto maps and they are easier to read and debug than transition tables and state variables. I couldn't write more than a hundred lines without using break, continue and return, which are just particular flavours of goto. I remember trying to code in Pascal and winding up with conditions spanning hundreds of lines, nested 8 or more deep, mainly because it wasn't allowed to branch out using a goto. The resulting mess of extra nesting and spurious state-holding variables was worse, IMO.

While the goto stuff exists in C language, there is no reason of not using it !

If it was'nt so good, it should have to disappear for a long time....

Grag38:
While the goto stuff exists in C language, there is no reason of not using it !
If it was'nt so good, it should have to disappear for a long time....

Well, as with anything, there are definitely circumstances where it is inappropriate to use gotos, just as torturing a for () into an if () and such can make things tough to follow. Goto can technically be used to branch into the middle of control structures, but the resulting functionality is impossible to debug, and this is the type of thing that made folks close the door on it originally. But C, and especially C++ are littered with stuff that is so limited in usefulness that I am not sure they should stay. Bit-fields and structure return types are things I scratch my head at. Digraphs are an outright menace. Some people say that goto, also, aught to go, but I say oh no, it isn't so. Judicious gotos, to and fro, are apropos and handy for the pro.

Legacy support is a major sticking point in the C/C++ language. My employer alone has hundreds of thousands of lines of code written decades ago that still compiles with Visual Studio 2010. Add that to the thousands of companies out there with similar code bases, and any changes to the standard that could potentially render millions of lines of code uncompilable is met with incredible resistance.

void loop() 
{
  String c="loopk";
  if (c == "loopk")
    loopk();
}
void loopk()
{
  Serial.print("strings can be used as function names");
}

There, I fixed it. :slight_smile:

Or, with function pointers and lookup table:

struct funcLookup{
  char name[10];
  void (*function)(int);
};

int x = 10;
funcLookup funcTable[3];

void setup(){
  strcpy(funcTable[0].name, "func1");
  strcpy(funcTable[1].name, "func2");
  strcpy(funcTable[2].name, "func3");
  
  funcTable[0].function = &func1;
  funcTable[1].function = &func2;
  funcTable[2].function = &func3;
  
  Serial.begin(115200);
  
  char funcName[] = "func2";
  
  callFunc(funcName, 10);
  
}

void loop(){
  
  
}

void callFunc(char* funcName, int val){
  for(int i = 0; i < 3; i++){
    if(strcmp(funcName, funcTable[i].name) == 0){
      funcTable[i].function(val);
      return;
    }
  }
}

void func1(int val){
  Serial.println("Function 1");
  Serial.println(val);
}

void func2(int val){
  Serial.println("Function 2");
  Serial.println(val);
}

void func3(int val){
  Serial.println("Function 3");
  Serial.println(val);
}

Well, function names can be used as strings in a sense: