Hi, there I am trying to write a complicated code (at least for my knowledge of C++ and Arduino). In my code, i am and wondering if I can write a function in another function and possibly a third function. The code is about moving stepper motors and measuring the distance using an ultrasonic sensor. Before I start writing the code I want to make sure that it will work. If you see any problem with this working feel free to let me know.
Thanks for any help.
Luke_Cap:
Hi, there I am trying to write a complicated code (at least for my knowledge of C++ and Arduino). In my code, i am and wondering if I can write a function in another function and possibly a third function. The code is about moving stepper motors and measuring the distance using an ultrasonic sensor. Before I start writing the code I want to make sure that it will work. If you see any problem with this working feel free to let me know.
Thanks for any help.
you can call a function from a function... google stack to learn about how memory is consumed in a function calling a function calling a function...
also, thanks to C++ you can also define a function in a function (though you won't see this on this forum much!). it has a weird name to match its weird syntax.... Lambda.
example that shows both a function calling a function (yes, loop() and setup() are functions) and a lambda:
void setup() {
Serial.begin(9600);
}
void loop() {
auto funct = []()->float{return (analogRead(A0) * 1.12345);};
Serial.println(funct(), 3);
// note the trailing parens when done in line as follows
Serial.println([]()->float{return (analogRead(A0) * 1.12345);}(), 3);
delay(1000);
}
If functions belong together, then they should be in a class. Classes also allow you to have private functions which can't be called from outside the class.
For example, you have high level functions like moveMotor() and findPosition() that will be accessed from the main program which uses the stepper motor to do something useful. But there will be low-level functions like moveOneStepForwards() which should never be called from the main program. You make those functions private to the class.
Luke_Cap:
I am and wondering if I can write a function in another function and possibly a third function.
You can call other functions from within a function but it is much much simpler to define all the functions separately. Using Lambdas is very head-numbing stuff.
Robin2:
I remain to be convinced. The Ruby folks like it - a program that change itself. But it is too easy to end up with a mess that nobody can figure out.
...R
It is a nice way to create a function that would otherwise never be called from anywhere else in your program. It's just another tool in the bag.
no need to go and find that function (and there are certainly no other callers).
But if you are extending the paradigm into other languages, try to imagine Javascript (or even Python) without anonymous functions... who want to live in that world?
The teacher needs to be match his advice to the skill level of the student.
...R
learning doesn't happen only like that... the best teachers challenge their students to learn more. The best students are intrigued by learning more.
Giving a more thoughtful or thorough answer may 1) provoke further learning 2) show other people options and 3) teach other people new ways. It is the reason a forum exists, after all.
BulldogLowell:
Giving a more thoughtful or thorough answer may 1) provoke further learning 2) show other people options and 3) teach other people new ways.
To a certain extent. Someone whose experience level is such that they don’t know nested (normal) functions aren’t allowed in the C language is probably not ready to learn about Lambda functions. Attempting to do so would most likely end up being a confusing distraction.