Functions in Functions

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.

moveMotor(x)
findPostion()


moveMotor[
countRepetitions=0

      while(countRepetitions<x)
          stepper motor moves 
         
          findPostion() 
          countRepetitions=++

]

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.

Have a look at Planning and Implementing a Program

...R

Using Lambdas is very head-numbing stuff.

Because of all the banging your head against the wall trying to get the syntax right.

Whoever invented that mess was smoking crack while doing it.

PaulS:
Because of all the banging your head against the wall trying to get the syntax right.

Whoever invented that mess was smoking crack while doing it.

then you learn it and you realize how helpful it is.

not pretty, but goddam useful!

BulldogLowell:
then you learn it and you realize how helpful it is.

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

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.

useful in Arduino at times as well

attachInterrupt(digitalPinToInterrupt(somePin),[](){someVar++;}, FALLING);

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?

who want to live in that world?

Me. I hate not seeing that a function is called, and being able to find where the function is defined, and exactly what it does.

Lambda functions look like a cartoon character swearing.

None of which is germane to the OP’s question given the experience level to be inferred from it.

gfvalvo:
None of which is germane to the OP’s question given the experience level to be inferred from it.

really?

Luke_Cap:
...i am and wondering if I can write a function in another function and possibly a third function.

I believe it is exactly addressing the questions of the OP; forum members demonstrated ways to do what OP was wondering...

PaulS:
Lambda functions look like a cartoon character swearing.

well then, {} off!

:wink:

BulldogLowell:
really?

Yes. Really.

The teacher needs to be match his advice to the skill level of the student.

...R

Robin2:
Yes. Really.

The teacher needs to be match his advice to the skill level of the student.

...R

Bullseye !

Robin2:
Yes. Really.

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.

BulldogLowell:
learning doesn't happen only like that... the best teachers challenge their students to learn more.

Agreed. But the challenge should be just a single step beyond the current level of competence.

...R