Simple question but I thought it would be easier to ask...

I am learning on the Uno and have made a simple 7 segment display sketch and made a function for each "number" so the right segments light when that numbers function is called.

Can a function name have a variable in it? or an array?

right now I have;

void loop(){
numberOne();
numberClear();
numberTwo();
numberClear();
numberThree();
numberClear();
numberFour();
numberClear();
numberFive();
numberClear();

Rather than call each number individually, I would like to just use a for loop for example.

Thanks

Jeff
KC9QQM

Please post your entire code (that includes declarations and definitions , setup etc ) when asking software questions .

you most certainly can do what you want with function pointers.

I did this example for someone here earlier....

note how you can use it in a for loop in this simple example, enter an 'X' into the serial display to see it function:

void (*myFunction[2])(void);

void setup()
{
  Serial.begin(9600);
  myFunction[0] = myFunction1;
  myFunction[1] = myFunction2;
  pinMode(13, OUTPUT);
}
void myFunction1(void)
{
  Serial.println("Function ONE");
  digitalWrite(13, !digitalRead(13));
}
void myFunction2(void)
{
  Serial.println("Function TWO");
}
void loop()
{

  if (Serial.available() > 0)
  {
    char myChar = Serial.read();
    if (myChar == 'X')
    {
      for (int i = 0; i < 2; i++)
      {
        myFunction[i]();//<<<<<<<<<<<<<<<<<<<<<<<<<<  function call here<<<<<<<<<<<
      }
    }
  }
}

another example using on-board LED and either a button press or entering a character into the serial monitor

#include <math.h>

#define NUMBER_OF_ROUTINES 10
#define DEBUG

#ifdef DEBUG
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINT(x)   Serial.print(x)
#else 
#define DEBUG_PRINTLN(x) 
#define DEBUG_PRINT(x) 
#endif

void (*ledFunction[NUMBER_OF_ROUTINES])(void);

byte ledPin = 13;
byte buttonPin = 2;
byte lastPressed;
byte state = 0;
byte increment = 5;
byte briteness;
unsigned long startTime;
boolean printed = false;
//
void setup() 
{    
  ledFunction[0] = ledFunction0;
  ledFunction[1] = ledFunction1;
  ledFunction[2] = ledFunction2;
  ledFunction[3] = ledFunction3;
  ledFunction[4] = ledFunction4;
  ledFunction[5] = ledFunction5;
  ledFunction[6] = ledFunction6;
  ledFunction[7] = ledFunction7;
  ledFunction[8] = ledFunction8;
  ledFunction[9] = ledFunction9;
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); 
}
//
void loop()
{
  int pressed = digitalRead(buttonPin); 
  if (pressed == LOW)
  {
    if (pressed != lastPressed)
    {
      state++;
      printed = false;
    }
  }
  lastPressed = pressed;
  if (Serial.available())
  {
    char myChar = Serial.read();
    {
      state++;
      printed = false;
    }
  }
  if (state >= NUMBER_OF_ROUTINES) state = 0;
  ledFunction[state]();
}

void ledFunction0()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Math Fade"));
    printed = true;
  }
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(ledPin, val);
}
//
void ledFunction1()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Fast Flash"));
    printed = true;
  }
  if (millis() - startTime >= 100UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime += 100UL;
  }
}
//
void ledFunction2()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Slow Flash"));
    printed = true;
  }
  if (millis() - startTime >= 500UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime += 500UL;
  }
}
//
void ledFunction3()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("2.5% Duty Cycle Flash"));
    printed = true;
  }
  unsigned long nowMillis = millis();
  if (nowMillis - startTime < 25UL)
  {
    digitalWrite(ledPin, HIGH);
  }
  if (nowMillis - startTime < 1000UL)
  {
    digitalWrite(ledPin, LOW);
  }
  else
  {
    startTime += 1000UL;
  }
}
//
void ledFunction4()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Quick Fade"));
    printed = true;
  }
  if (millis() - startTime >= 10UL)
  {
    briteness += increment;
    if (briteness >= 255 || briteness <= 0) increment = - increment;
    analogWrite(ledPin, briteness);
    startTime += 10UL;
  }
}
//
void ledFunction5()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Slow Fade"));
    printed = true;
  }
  if (millis() - startTime >= 35UL)
  {
    briteness += increment;
    if (briteness >= 255 || briteness <= 0) increment = - increment;
    analogWrite(ledPin, briteness);
    startTime += 35UL;
  }
}
//
void ledFunction6()
{
  static boolean fadeNow;
  if (!printed)
  {
    DEBUG_PRINTLN(F("Fade Down and Up with Pause"));
    printed = true;
  }
  if (millis() - startTime > 1000UL)
  {
    fadeNow = true;
    briteness = 255;
    increment = -5;
  }
  if (fadeNow)
  {
    for (int i = 0; i < 2; i++)
    {
      for (int j = 0; j <= 255; j += 5)
      {
        analogWrite(ledPin, briteness);
        briteness += increment;
        delay(25);
      }
      increment = - increment;
    }
    fadeNow = false;
  }
}
//
void ledFunction7()
{
  static boolean fadeNow;
  if (!printed)
  {
    DEBUG_PRINTLN(F("Fade Up then Down with Pause"));
    printed = true;
  }
  if (millis() - startTime > 1000UL)
  {
    fadeNow = true;
    briteness = 0;
    increment = 5;
  }
  if (fadeNow)
  {
    for (int i = 0; i < 2; i++)
    {
      for (int j = 0; j <= 255; j += 5)
      {
        analogWrite(ledPin, briteness);
        briteness += increment;
        delay(25);
      }
      increment = - increment;
    }
    fadeNow = false;
  }
}
//
void ledFunction8()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Led On"));
    printed = true;
  }
  digitalWrite(ledPin, HIGH);
}
//
void ledFunction9()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Led Off"));
    printed = true;
  }
  digitalWrite(ledPin, LOW);
}