How to pass parameters between functions?

Hello,
I'm trying to mess around with some code to learn how to do functions. Here I want to create two functions where:
function 1 (called "task1") monitors the state of a switch and stores it in a variable called "state".
function 2 (called "task2") reads "state" from function 1 and displays it only if "state" is 0.
This is my code:

byte SW1_GPIO = 22; //digital input (i.e. switch)
bool state;         //state of the switch

//Task1 - Monitor switch state
bool Task1() {
  state = digitalRead(SW1_GPIO); //read state of switch
  return state;
}
//Task2 - print state to the serial port  ONLY when the pushbutton(in Task 2) is pressed.
void Task2(state) {
  if (state == 0) {
    Serial.print(state);
  }
  else {
    Serial.println("no button press");
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(SW1_GPIO, INPUT);

  void loop() {
    Task1();
    Task2(state);
  }

So I know if I want to pass a parameter I should include it in the function brackets, which I've done. I get the error "variable or field 'Task2' declared void", which I don't understand why because Task2 is not returning anything, that's why I put it as void. What's the right way to go about what I'm trying to do?

Thank you for your help :slight_smile:

You need to tell the compiler the type of "state"

void Task2(int state) {

digitalRead does not return a bool

1 Like

Good, I didn't know that. It works now! :smiley:Thank you.

Also, if you're going to return yhe value, there's no need to assign it to a variable before the return(unless you need the global value elsewhere (which you shouldn't really))

return digitalRead(SW1_GPIO);

Your sketch of post #1 suffers from compilation error; because, the setup() function misses a balancing parenthesis.

1 Like

Yes, we know. That's what the topic is about.

1 Like

if I don't assign it to a variable (in this case, "state") then I don't know how it's possible to send it as a parameter to the task2()? unless it's something like void Task2(digitalRead())... :face_with_spiral_eyes:

Just spotted that, cheers.

If "state" is a global, then there's little point passing it to or from a function.

void loop() 
{
    bool task1State = Task1();
    Task2(task1State);
  }
1 Like

Oh I see what you mean. In post #1 it shows I've declared it as global so I don't need to pass it to task2 at all

Would appreciate if you post the final version of your sketch. This is to see your tricks of passing parameter (s) to user-defined function.

Yes, this is my final code:

byte SW1_GPIO = 22; //digital input (i.e. switch)
int state;         //state of the switch

//Task1 - Monitor switch state
int Task1() {
  state = digitalRead(SW1_GPIO); //read state of switch
}
//Task2 - print state to the serial port  ONLY when the pushbutton(in Task 2) is pressed.
void Task2() {
  if (state == 0) {
    Serial.println(state);
  }
  else {
    Serial.println("no button press");
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(SW1_GPIO, INPUT);
}

void loop() {
  Task1();
  Task2();
}

Why is "bool?" The digitalRead() returns HIGH or LOW; so, the data type should be int or byte.

Because I assume the author has taken care of the conversion.

1 Like

Few comments on your sketch of post #12:

1. At the top of the program, you can include the following line as comment.
//Button is connected with an external pull-up resistor.

2. Because Arduino IDE makes the forward declaration (also known as function prototyping) of the user-defined functions, then the Task1() and Task2() functions may come after the loop() function.

3. Because you have declared Task1() function with an int-type return, then the calling program must have the following format as per post #9 of @anon73444976 to collect the return value. Now, there is no need of the global variable, state. You can declare it locally in Task1() function; as a result, the value of state variable will be copied to task1State variable. (See sketch in Step-9.)

int task1State = Task1();

4. Following post #9, you may call Task2() function with parameter task1State (called actual parameter/argument).

==> Task2(task1State); //nothing will come from the called function

5. You need to declare a variable (say: int y) in the "formal argument field" of the called program (Task2()); where, the value of the actual argument (task1State) will be copied to variable y.

==> void Task2(int y)
{

6. The digitalRead() function returns HIGH or LOW which should be grasped by int or byte type variable. This is the reason to declare "int y" in Step-5.

7. Now, we see the following:
The variable state is assigned a value by digitalRead() function; it is copied to variable task1State which is passed to Task2() function via variable y and then y is compared with LOW to take decision.

8. Lastly, you may insert some delay(2000) in the loop() function to observe smooth appearance of message on the Serial Monitor.

9. The Refined Sketch (tested):

//Button is connected with an external pull-up
byte SW1_GPIO = 22; //digital input (i.e. switch)

void setup() 
{
  Serial.begin(9600);
  pinMode(SW1_GPIO, INPUT); 
}

void loop() 
{
  int task1State = Task1();
  Task2(task1State);
  delay(2000);
}

int Task1() 
{
  int state = digitalRead(SW1_GPIO); //read state of switch
  return (state);
}

void Task2(int y) 
{
  if (y == LOW) 
  {
    Serial.println("Button is pressed.");
  }
  else 
  {
    Serial.println("No Button is pressed.");
  }
}
1 Like

"return" is not a function; the parentheses are superfluous.

const byte SW1_GPIO = 22; // switch is connected between pin and GND

void setup() 
{
  Serial.begin(9600);
  pinMode(SW1_GPIO, INPUT_PULLUP);
}

void loop() 
{
  Task2(Task1 ());
  delay(2000);
}

int Task1() 
{
  return digitalRead(SW1_GPIO); 
}

void Task2(int y) 
{
   Serial.print(F("Button is "));
   Serial.print ((y == LOW) ? F("") : F("not"));
   Serial.println(" pressed.");
}

(uncompiled, untested)

As return is a keyword, I think that the compiler will not be confused to treat return() as a function. I like this style because of this: sizeof (identifier) which many users are found to exercise.

Tested! Working Fine!! Would prefer to see a line feed between the two messages which the OP will do, hopefully!

Output:

Button is 
not pressed.
Button is 
 pressed.

Question to OP:
Do you know the working principle of the following line of code of post #16?

Serial.print ((y == LOW) ? F("") : F("not"));

sizeof is not a function either, and (somewhat strangely) parentheses are not always required.

I don't like it, I would not even consider it a stile.

Also, I see no connection between return and sizeof.

1 Like

Though it is an operator and yet novice might accept it as a function due to parentheses at the end and the terminating character.