Pages: [1] 2   Go Down
Author Topic: String as a function name ??  (Read 1381 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i simply want to use a branch statement
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
String c="loopk";
  c();

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

C doesn't have a branch statement.

Does it have a goto statement?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


Ontario
Offline Offline
God Member
*****
Karma: 24
Posts: 862
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Ontario
Offline Offline
God Member
*****
Karma: 24
Posts: 862
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

There, I fixed it.  smiley
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1] 2   Go Up
Jump to: