Arduino functions and passing arguments or variables to get things done.

https://esp32arduino.com/

arduino functions pass variables

This may be simple for you, and once upon a time, I probably understood it. But being over 70 years old and coming back to “programming” or “coding” or whatever you young folks call it, it has involved a lot of head scratching. I use this page as a reminder to myself, because by tomorrow I will have forgotten it already. But here is what I figured out about functions, and passing variables (or arguments) one way and the other in Arduino or ESP32 using the Arduino version of C++ . Maybe this will help someone.

Arduino functions and passing arguments or variables

  • BASIC
  • To call the function: myFunction( ) ;
  • write the function: void myFunction( ) { put code here }
  • RETRIEVE a value (Argument? Variable? Value? ) from the function
  • To call the function: int getValueBack = myFunction( );
  • write the function: int myFunction( ) { put code here and INCLUDE return someVariable; }
  • it can be “int” or “long” or “float” or whatever “type” you choose, but be consistent.
  • ACCEPT values INTO a function.
  • To call the function: myFunction( 243, someVariable );
  • write the function: void myFunction( int varOne, int varTwo ) { put code here, use varOne and varTwo }
  • GO BOTH WAYS
  • To call the function: int getValueBack = myFunction( passSomeValue , 674);
  • write function: myFunction(int getOne, getTwo) { code using getOne & getTwo AND return sendSomeValue }

Some more thoughts on using arduino functions and passing arguments or variables.

  • The function you write goes “below” the “void loop()” function not in it, but you CALL your function from the void loop() or from another function.
  • don’t forget you use curly braces { } for the code, but the function names NEED ( ) regular braces
  • use your semicolon ; when “calling” the function but don’t use one between the function and the curly braces when you write the function

Lots of help has come from:

https://www.the-diy-life.com/using-arduino-functions-correctly-make-code-more-efficient/
https://youtu.be/VHK69SB3lo4

Any discussion of comments can take place in my facebook group here
https://www.facebook.com/groups/432605008529680
my youtube channel is here https://www.youtube.com/channel/UC5SzL2JDSYAn0joPd0OXEFw

Loading

Please share it. Thanks!