Go Down

Topic: String as a function name ?? (Read 1 time) previous topic - next topic

preejit

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");
}

AWOL

Quote
("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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

preejit

i simply want to use a branch statement

AWOL

C doesn't have a branch statement.
You're going to have to tell us more.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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


Nick Gammon


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


Code: [Select]
String c="loopk";
  c();


You can't do that in assembler, but whatever.
http://www.gammon.com.au/electronics

Nick Gammon


C doesn't have a branch statement.


Does it have a goto statement?
http://www.gammon.com.au/electronics

AWOL

Yes. But we don't talk about it in poilte circles.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon

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.
http://www.gammon.com.au/electronics

jraskell


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


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

gardner


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.

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....

gardner


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.

jraskell

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.

johnwasser

Code: [Select]

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


There, I fixed it.  :)
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up