Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First you need to learn how scope works in C, and how are variables passed to functions. Local Scope > Global Scope, for example, if you have a global variable named "variable", and a function with a variable also named "variable", you'll be working on the local copy of it.</p> <p>Now, with that in mind, think of this, when you pass an argument to a function, you actually make a copy of the variable, with local scope. So in your function:</p> <p>check(4, some_global_boolean, 'a');</p> <p>When you defined the function you gave each argument a name right? so when you call a function, and you instantiate each argument into a variable(of the defined type) according to the function signature. Kinda this: argument1 = value1, argument2 = value 2,..., argumenn = valuen</p> <p>Now, lets take this to your function: boolean check(int key, boolean prev_key_high, char c) => key = 4, prev_key_high = some_global_boolean, c = 'a'</p> <p>Each of this variables has a local scope, and it has the same values as the arguments/variables/values you used in the call, but the variable is in a completely different position in memory. </p> <p>Now, there are different ways to deal with this, personally I discourage people into using global variables:</p> <p>1.Instantiate a variable in your main function (static or dynamic as you wish), and work with pointers to that specific variable:</p> <pre><code> int main(int argc, char const *argv[]) { boolean some_global_boolean = true; //or false or w/e or initialize it as you wish check(4, &amp;some_global_boolean, 'a'); return 0; } boolean check(int key, boolean * prev_key_high, char c){ if (key == LOW) { if ( *prev_key_high){ *prev_key_high = false; Serial.println(c); return true; } } else { *prev_key_high = true; return false; } } </code></pre> <p>2.If you insist in using a global variable, use it directly, or in combination with previous comment. I encourage you into learning about pointers and working with them.</p> <pre><code>boolean check(int key, char c){ if (key == LOW) { if ( some_global_boolean){ some_global_boolean = false; Serial.println(c); return true; } } else{ some_global_boolean = true; return false; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload