Sorry, if I'm not in the right topic, but I'm new to C++ programming, as well as arduino.
I know how to do this in javascript or php, but microcontroller is still mistery to me.
What I'd like to achieve is to have a function which includes logic for example a simple led blinking, but I'd like to pass that function in other functions with different variable, to be more specific, there's a code I'd like to achieve:
//Functions
void checkPrevious();
void setupFunction();
void blinkingLED();
//Variables
int redLED = 14;
int greenLED = 13;
int variable;
void setup(){
// NOT GOING TO INCLUDE THINGS HERE, UNNECESSARY I THINK
}
void loop(){
checkPrevious();
setupFunction();
}
void blinkingLED(variable){ //<- I want to pass variable into this function
digitalWrite(variable, HIGH);
delay(250);
digitalWrite(variable, LOW);
delay(250);
}
void checkPrevious(variable){
blinkingLED(redLED); // <- I want to pass redLED into blinkingLED function.
}
void setupFunction(){
blinkingLED(greenLED); // <- I want to pass greenLED into blinkingLED function.
}
When you declare the function you have to specify the type of each argument.
Note: You aren't actually using the argument in checkPrevious() and you don't pass a value for it when you call it so it is better to just eliminate the argument. If you keep the argument and add a data type you will have to change the call to pass an argument of the specified type.
void blinkingLED(int variable){ //<- I want to pass variable into this function
void checkPrevious()
Make those two changes (add 'int' one place, remove 'variable' in the other) and your sketch compiles without error.
They are needed for C and C++ programming but the Arduino IDE will (try to) generate those for you so you very seldom have to put them in yourself. That, and adding "#include <Arduino.h" at the top of your sketch, are ways the Arduino IDE tried to make C++ programming easier for non-programmers.
In your example code you do not pass a function, instead you call this function (in a regular way). Your example looks fine - what do you want to accomplish?
What do you mean by "share variables between functions"?
You can forward variables (their content) to other functions, which can also forward to other functions.
Your question means actually: shared variables in functions (which are "global" and outside any function.
Sure, a global variable (as shared) can be used in any function (which can see it). Not a good practice but works without to ask about "function pointers".
Passing a function (which is a pointer to the code) is possible. But you had to declare the function called with a parameter taken, which is a function pointer. It works actually just via using typedefs (because function pointers are a bit different and risky: do not call any memory address for data - it must the code address of a function).
If you pass a function pointer to another function - the function called does not have any parameters you would need to call this function via the pointer.
You had to add also the parameters for this function called via its pointer (forward). So, you would still forward the parameters (just which function is called becomes variable).
Actually, it does not make so much sense:
The function you want to call with a pointer to the other function called inside, and all the parameters provided on the top function is only usable for this specific function (at least with the same semantic and parameter list). At the end: it is just a simple sub-function call as you can write in the regular way (just forward the parameters and call the final function).
Here a sample code (as C, works also in C++):
typedef int (*T_MyFunc)(int par1); //define a function pointer
int MyFunc(int par1) //the same 'signature' as the typedef!
{
//do something with par1
return ++par1;
}
int CallForward(T_MyFunc fPtr, int par1) //use the function pointer
{
return fPtr(par1);
}
int CallForwardAsTheSame(int par1) //will do exactly the same
{
return MyFunc(par1);
}
You call it this way:
CallForward(MyFunc, 1);
BTW: with C++ and using inheritance, polymorphism - you can call (even let call) other functions much easier (e.g. inherited from a base class, with different types and numbers of arguments).