Creating a function with default paramaters

I have a function used to read the value of an analog pin that a reflection sensor is connected to. Normally, the function is this:

int senseReflection(int pinval) { reflectVal = analogRead(pinval); return reflectVal; } senseReflection(reflectPin);

Where reflectPin is the the pin the sensor is connected to, and reflectVal is the value variable. Normally, I would use senseReflection(reflectPin); but I can replace reflectPin with any value. Is there any way I can have a default value, such that if I were to use senseReflection(); it would interpret it as senseReflection(reflectPin); but still allow me to do use a different paramater, such as senseReflection(A2); ?

You need two functions one called sensereflection(); and one called sensereflection(int pin);

winner10920: You need two functions one called sensereflection(); and one called sensereflection(int pin);

Um.. no?

This is the syntax for default values:

functionreturn funcationanem(argtype argname = default, ...) {
    ...
}

So basically you add an "= reflectPin" after pinVal.

However, default values does bring you into the area of a small bug in the IDE: You'll need to define the function prototype yourself. To do that, just copy and paste the function before the opening brace (so, in your case "int senseReflection(int pinval)") and add a semicolon before it. Put the prototype at the beginning of your sketch.

You can give a default argument value in the function declaration:

int senseReflection(int pinval = A5)
  {
  int reflectVal = analogRead(pinval);
  return reflectVal;
  }
  
void setup ()
  {
  senseReflection (5);
  senseReflection ();
  }
  
void loop () {}

(edit) Ninja'd by zero seconds!

Ahh, I learned something new too, I was thinking like when you make libraries and have multiple functions with different number of parameters

Ahh, I learned something new too, I was thinking like when you make libraries and have multiple functions with different number of parameters

I think you mean different [u]kinds[/u] of parameters, such as the Print::print() method being able to handle double, int, char, char *, byte, etc.

That and when I want defaults like say one could be

beginserial(int baudrate){
Serial.begin(baudrate);
}
//or
beginserial(int baudrate, int port){
If(port==1) serial1.begin(baudrate);
//etc port = n, begins serial n
}