Tuesday, 17 December 2013 21:51

Arduino function with optional argument(s)

Written by
Function Declaration and Function Prototypes: function prototype allowing the function to be used before it is defined.
functionReturnType myFunctionName(optionalArgumentType optionalArgument = defaultValue);
 
Function Implementation

functionReturnType myFunctionName(optionalArgumentType optionalArgument);
{
   // put your code here
}

 
Function calls

myFunctionName(); // Use the default value
myFunctionName(1); // Use specific value

 
Example
  1. int myFunction(int optionalArgument = 1);
  2.  
  3. void setup() {
  4. Serial.begin(9600);
  5. }
  6.  
  7. void loop() {
  8. Serial.println(myFunction()); // Use the default value
  9. Serial.println(myFunction(2)); // Use specific value
  10. }
  11.  
  12. int myFunction(int optionalArgument)
  13. {
  14. return optionalArgument;
  15. }
 
Read 15212 times Last modified on Tuesday, 17 December 2013 22:12

Leave a comment

Back to Top