is it possible to use variables when calling functions?

I have a number of similar functions that I want to call I am doing the following at the moment

   input1 = digitalRead(pin1);  input2 = digitalRead(pin3); 
   input3 = digitalRead(pin4); input4 = digitalRead(pin6);  
  if (input1 == 0){  bitWrite (hex,0,1);}  else{  bitWrite (hex,0,0);}
  if (input4 == 0){  bitWrite (hex,1,1);}  else{  bitWrite (hex,1,0);}
  if (input2 == 0){  bitWrite (hex,2,1);}  else{  bitWrite (hex,2,0);}
  if (input3 == 0){  bitWrite (hex,3,1);}  else{  bitWrite (hex,3,0);}

this puts a number between 0 and 15 into hex (is there an instruction that reads four inputs and gives a number between 0 and 15?)

  if (hex == 0)   lc.clearDisplay(0);       if (hex == 1) writeArduinoOnMatrix1(); 
  if (hex == 2) writeArduinoOnMatrix2();  if (hex == 3)  writeArduinoOnMatrix3();
  if (hex == 4) writeArduinoOnMatrix4(); if (hex > 4)  lc.clearDisplay(0);

this then runs function writeArduinoOnMatrixX (where X is the number)
the function will then loop and continue running until the inputs and hence hex are altered.
Is there any way I can substitute the number after writeArduinoOnMatrix with writeArduinoOnMatrix-hex-?
I have only four instances in the example but there will be 16 in the finished program and being able to use the variable at the end will save lots of lines. I could use something like

if (hex ==0) lc.clearDisplay(0);  else  writeArduinoOnMatrix'hex';

thanks.

This is at least the third time this week that a very similar question has been asked. The short answer is no. The long answer is function pointers in an array, and call the function at the appropriate index position, by pointer.

Example of what PaulS said:

void writeArduinoOnMatrix1 ()
 {
 Serial.println (1);
 }
 
void writeArduinoOnMatrix2 ()
 {
 Serial.println (2);
 }

void writeArduinoOnMatrix3 ()
 {
 Serial.println (3);
 }

void writeArduinoOnMatrix4 ()
 {
 Serial.println (4);
 }

typedef void (*GeneralFunction) ();

// array of functions
GeneralFunction writeArduinoOnMatrixX [4] =
 {
 writeArduinoOnMatrix1,
 writeArduinoOnMatrix2,
 writeArduinoOnMatrix3,
 writeArduinoOnMatrix4,
 };

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  for (int i = 0; i < 4; i++)
    writeArduinoOnMatrixX [i] ();
  }  // end of setup

void loop () { }

But can't you just pass a variable down to a single function and let it decide what to do?

thank you Nick.
What I am doing is using a max7219 to run an 8 x 8 matrix. the writeArduinoOnMatrixX function sends an array of info to the max7219 depending on which number it gets from the variable 'hex' sent from a hexidecimal switch.

void writeArduinoOnMatrix3() {             
  static byte a[8]={B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,
B10000001};
     displayThem(a);

it then calls function displayThem

void displayThem(byte a[]) { lc.setRow(0,0,a[0]);  lc.setRow(0,1,a[1]);  
               lc.setRow(0,2,a[2]);lc.setRow(0,3,a[3]);  lc.setRow(0,4,a[4]);  
               lc.setRow(0,5,a[5]); lc.setRow(0,6,a[6]);   lc.setRow(0,7,a[7]);}

each writeArduinoOnMatrix function is quite different but the displayThem function is the same so I don't think

But can't you just pass a variable down to a single function and let it decide what to do?

would work.
I think the code you sent me cycles through all the writeArduinoOnMatrix functions (I knew I should have given it a shorter name) one after the other. I want it to stay on one function.

I think the code you sent me cycles through all the writeArduinoOnMatrix functions (I knew I should have given it a shorter name) one after the other. I want it to stay on one function.

It was only an example to illustrate how to call the 4 different functions by index number. Call them however you need to.

Enough of these snippets, already.

http://snippets-r-us.com/

Post all your code.

matelot:
(is there an instruction that reads four inputs and gives a number between 0 and 15?)

If you organize your inputs onto adjacent pins in the same Atmega port you can read all at the same time using PINx where x is the port number. For example to get a 0-15 number from pins 8-11 on an Uno you just need two lines of code

byte myVal = PINB;
myVal &= 0b00001111; // to zero the irrelevant bits 7-4

Also, (unless I am missing something) you seem to be making a bit of a mountain out of the business of selecting which set of bit values you want to output.

Why not define all the values in a 2-D array at the same time and then just use the input value to select the relevant row of the array? (not sure if this is the right way to do it - but someone else will quickly correct me)

byte a[]={
                     {B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001} ,
                    {B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001}
                  }

Then your code would be something like this

void displayThem(byte row) { lc.setRow(0,0,a[row][0]);  .....

...R

OK nick.
I wasn't too bothered about the rest of the program, i.e. the 4 inputs and you have answered my problem about repeating the write to matrix function but the rest of the program (all of it as far as I have got) is below.

//We always have to include the library
#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);
int pin1 = 2;
int pin3 = 3;
int pin4 = 4;
int pin6 = 5;
//pins with 0 on the left
//      4  5  6
//
//      1  2  3
//connect vcc to pins 2 and 5
int input1 = 0;
int input2 = 0;
int input3 = 0;
int input4 = 0;
int hex;
byte a[8];
/* we always wait a bit between updates of the display */
unsigned long delaytime=10;

void setup() {
  
Serial.begin(9600);
pinMode(pin1, INPUT);
pinMode(pin3, INPUT);
pinMode(pin4, INPUT);
pinMode(pin6, INPUT);  
/*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,1);
  /* and clear the display */
  lc.clearDisplay(0);
}
void writeArduinoOnMatrix1() {
  /* here is the data for the characters */
    static byte a[8]={B00111100,B01111110,B11100111,B11001011,B11010011,B11100111,B01111110,B00111100,};
//  lc.setRow(0,0,a[0]);  lc.setRow(0,1,a[1]);  lc.setRow(0,2,a[2]);  lc.setRow(0,3,a[3]);
//  lc.setRow(0,4,a[4]);  lc.setRow(0,5,a[5]);  lc.setRow(0,6,a[6]);  lc.setRow(0,7,a[7]);
    displayThem(a);
}  
void writeArduinoOnMatrix2() {             
    static byte a[8]={B11111111,B11111110,B11100000,B11010000,B11001000,B11000100,B11000010,B10000001,};
//  lc.setRow(0,0,a[0]);  lc.setRow(0,1,a[1]);  lc.setRow(0,2,a[2]);  lc.setRow(0,3,a[3]);
//  lc.setRow(0,4,a[4]);  lc.setRow(0,5,a[5]);  lc.setRow(0,6,a[6]);  lc.setRow(0,7,a[7]);
    displayThem(a);
}
void writeArduinoOnMatrix3() {             
  static byte a[8]={B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001};
//   lc.setRow(0,0,a[0]);   lc.setRow(0,1,a[1]);   lc.setRow(0,2,a[2]);   lc.setRow(0,3,a[3]);
//   lc.setRow(0,4,a[4]);   lc.setRow(0,5,a[5]);   lc.setRow(0,6,a[6]);   lc.setRow(0,7,a[7]);
     displayThem(a);
}
void writeArduinoOnMatrix4() {
 // here is the data for the characters
  static byte a[8]={B00011000,B00111000,B00111000,B00011000,B00011000,B00011000,B00111100,B00111100};
//   lc.setRow(0,0,a[0]);   lc.setRow(0,1,a[1]);   lc.setRow(0,2,a[2]);   lc.setRow(0,3,a[3]);
//   lc.setRow(0,4,a[4]);   lc.setRow(0,5,a[5]);   lc.setRow(0,6,a[6]);   lc.setRow(0,7,a[7]);
     displayThem(a);
}



//void writeArduinoOnMatrix4() {
//  diplayThem(B00011000,B00111000,B00111000,B00011000,B00011000,B00011000,B00111100,B00111100,);
//}
//void diplayThem(byte r0, byte r1, byte r2, byte r3, byte r4, byte r5, byte r6, byte r7){
//  lc.setRow(0,0,r0);  lc.setRow(0,1,r1);  lc.setRow(0,2,r2);  lc.setRow(0,3,r3);
//  lc.setRow(0,4,r4);  lc.setRow(0,5,r5);  lc.setRow(0,6,r6);  lc.setRow(0,7,r7);
//}


void displayThem(byte a[]) {
  lc.setRow(0,0,a[0]);  lc.setRow(0,1,a[1]);  lc.setRow(0,2,a[2]);  lc.setRow(0,3,a[3]);
  lc.setRow(0,4,a[4]);  lc.setRow(0,5,a[5]);  lc.setRow(0,6,a[6]);  lc.setRow(0,7,a[7]);
}


void loop() { 
  Serial.println (hex);
  input1 = digitalRead(pin1);
  input2 = digitalRead(pin3);
  input3 = digitalRead(pin4);
  input4 = digitalRead(pin6);  
  if (input1 == 0)
  {
    bitWrite (hex,0,1);
  }
  else
  {
    bitWrite (hex,0,0);
  }
  if (input4 == 0)
  {
    bitWrite (hex,1,1);
  }
  else
  {
    bitWrite (hex,1,0);
  }
  if (input2 == 0)
  {
    bitWrite (hex,2,1);
  }
  else
  {
    bitWrite (hex,2,0);
  }
  if (input3 == 0)
  {
    bitWrite (hex,3,1);
  }
  else
  {
    bitWrite (hex,3,0);
  }

  if (hex == 0)
    lc.clearDisplay(0);
  if (hex == 1)
    writeArduinoOnMatrix1();
  if (hex == 2)
    writeArduinoOnMatrix2();
  if (hex == 3)
    writeArduinoOnMatrix3();
  if (hex == 4)
    writeArduinoOnMatrix4();     
  if (hex > 4)
    lc.clearDisplay(0);


}

I have not yet altered it to include your improvements but , if you do look through the whole thing, you will see that as I will be getting a total of 16 options from my input the repeat of instructions in functions gets a bit long winded.
Instead of the if statements at the end of the main loop I wanted to just say 'goto requested function' i.e.
writeArduinoOnMatrix hex;
or however it would look.
Robin, I hope the bit at the top of the main loop explains what I am doing to get the inputted number into the variable hex.

Isn't this confusing? All these "a" variables?

byte a[8];
...
void writeArduinoOnMatrix1() {
  /* here is the data for the characters */
  static byte a[8]={
    B00111100,B01111110,B11100111,B11001011,B11010011,B11100111,B01111110,B00111100,  };
...
  displayThem(a);
}  
void writeArduinoOnMatrix2() {             
  static byte a[8]={
    B11111111,B11111110,B11100000,B11010000,B11001000,B11000100,B11000010,B10000001,  };
...
  displayThem(a);
}
void writeArduinoOnMatrix3() {             
  static byte a[8]={
    B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001  };
...
  displayThem(a);
}
void writeArduinoOnMatrix4() {
  // here is the data for the characters
  static byte a[8]={
    B00011000,B00111000,B00111000,B00011000,B00011000,B00011000,B00111100,B00111100  };
...
  displayThem(a);
}

Instead of trying to call these different functions, why not just display the correct pattern from an array? Like this:

// 4 lots of 8
const byte bitPatterns [4] [8] =
{
  { B00111100,B01111110,B11100111,B11001011,B11010011,B11100111,B01111110,B00111100, } , //  matrix 1 
  { B11111111,B11111110,B11100000,B11010000,B11001000,B11000100,B11000010,B10000001, } , //  matrix 2 
  { B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001, } , //  matrix 3 
  { B00011000,B00111000,B00111000,B00011000,B00011000,B00011000,B00111100,B00111100, } , //  matrix 4 
};  // end of bitPatterns

And display like this:

  if (hex >= 1 && hex <= 4)
      displayThem (bitPatterns [hex - 1]);
  else
     lc.clearDisplay(0);

And make the display function simpler:

void displayThem(const byte a[8]) 
  {
  for (byte i = 0; i < 8; i++)
    lc.setRow(0,i,a[i]);  
  }  // end of displayThem

Now the problem of calling lots of different functions quietly goes away. Finished code (not tested):

//We always have to include the library
#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);
int pin1 = 2;
int pin3 = 3;
int pin4 = 4;
int pin6 = 5;
//pins with 0 on the left
//      4  5  6
//
//      1  2  3
//connect vcc to pins 2 and 5
int input1 = 0;
int input2 = 0;
int input3 = 0;
int input4 = 0;
int hex;

// 4 lots of 8
const byte bitPatterns [4] [8] =
{
  { B00111100,B01111110,B11100111,B11001011,B11010011,B11100111,B01111110,B00111100, } , //  matrix 1 
  { B11111111,B11111110,B11100000,B11010000,B11001000,B11000100,B11000010,B10000001, } , //  matrix 2 
  { B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001, } , //  matrix 3 
  { B00011000,B00111000,B00111000,B00011000,B00011000,B00011000,B00111100,B00111100, } , //  matrix 4 
};  // end of bitPatterns

/* we always wait a bit between updates of the display */
unsigned long delaytime=10;

void setup() {

  Serial.begin(9600);
  pinMode(pin1, INPUT);
  pinMode(pin3, INPUT);
  pinMode(pin4, INPUT);
  pinMode(pin6, INPUT);  
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,1);
  /* and clear the display */
  lc.clearDisplay(0);
}

void displayThem(const byte a[8]) 
  {
  for (byte i = 0; i < 8; i++)
    lc.setRow(0,i,a[i]);  
  }  // end of displayThem

void loop() 
  { 
  Serial.println (hex);
  input1 = digitalRead(pin1);
  input2 = digitalRead(pin3);
  input3 = digitalRead(pin4);
  input4 = digitalRead(pin6);  
  if (input1 == 0)
  {
    bitWrite (hex,0,1);
  }
  else
  {
    bitWrite (hex,0,0);
  }
  if (input4 == 0)
  {
    bitWrite (hex,1,1);
  }
  else
  {
    bitWrite (hex,1,0);
  }
  if (input2 == 0)
  {
    bitWrite (hex,2,1);
  }
  else
  {
    bitWrite (hex,2,0);
  }
  if (input3 == 0)
  {
    bitWrite (hex,3,1);
  }
  else
  {
    bitWrite (hex,3,0);
  }

  if (hex >= 1 && hex <= 4)
      displayThem (bitPatterns [hex - 1]);
  else
     lc.clearDisplay(0);

}

I have no idea what this is doing, BTW:

 input1 = digitalRead(pin1);
  input2 = digitalRead(pin3);
  input3 = digitalRead(pin4);
  input4 = digitalRead(pin6);  
  if (input1 == 0)
  {
    bitWrite (hex,0,1);
  }
  else
  {
    bitWrite (hex,0,0);
  }
  if (input4 == 0)
  {
    bitWrite (hex,1,1);
  }
  else
  {
    bitWrite (hex,1,0);
  }
  if (input2 == 0)
  {
    bitWrite (hex,2,1);
  }
  else
  {
    bitWrite (hex,2,0);
  }
  if (input3 == 0)
  {
    bitWrite (hex,3,1);
  }
  else
  {
    bitWrite (hex,3,0);
  }