# Performing functions

Hello
Bit of a novice here
I want to perform functions based on the value of an integer.
I have ten function to perform and the integer is used to determine which one
Obviously I can test the integer ten times and perform the relevant function but this seems very inelegant and wasteful.
Is there a way to do this
Thank you
Ed

Have a look at switch...case.

Fasteddie:
Obviously I can test the integer ten times and perform the relevant function but this seems very inelegant and wasteful.

It is neither.

Use SWITCH/CASE if it is suitable and you prefer it. I suspect the code generated by the compiler will be much the same either way.

...R

Thank you for your responses
So I looked up SWITCH CASE
It seems to me that it might save some typing over a string of IF statements followed by a GOTO (no screaming about GOTO please). There will, however, be pretty much the same number of statements. Unless I am missing something it appears to be six of one and tho threes of the other.
I do appreciate your responses and, if nothing else. I have learned something
Regards
Ed

(no screaming about GOTO please)

DON'T USE goto

I have ten function to perform and the integer is used to determine which one

Use the integer as an index into an array of function pointers, but make sure you do some bounds checking.

Fasteddie:
I want to perform functions based on the value of an integer.

If you have ten different values and ten functions, then a 'byte' in the range 0...9 would be enough to identify the value.
And you could have an array of 10 "function pointers", and then call the function from that function pointer array, accordingly to the value of your variable.

Jurs
Thank you
I think this is the kind of thing I was looking for but I don"t know how to create an array of function pointers. Can you enlighten me
Regards
Ed

You can do it like this but it’s easy to get wrong and very dangerous. Safer to let the compiler generate a pointer table for your switch/case when it determines that a table is the most efficient method.

``````void (*FunctionList)()[] = {FunctionOne, FunctionTwo, FunctionThree, FunctionFour};

for (int i=0; i<4; i++ ) {
FunctionList[i]();
}
``````

Don’t use ‘goto’. Use ‘else’.

``````if (value == 0) {
//code for 0
} else
if (value == 1) {
//code for 1
} else
if (value == 2) {
//code for 2
} else
if (value == 3) {
//code for 3
} else
if (value == 4) {
//code for 4
} else {
// default if nothing matches
}
``````

Fasteddie:
I think this is the kind of thing I was looking for but I don"t know how to create an array of function pointers. Can you enlighten me

That is certainly an option.

But the question in my mind is "what problem are you trying to solve here?"

If it is just to reduce the amount of typing then function pointers are a very complicated way to achieve that.

...R

Thank you for all your responses
I agree that the function list is a bit of an overkill
I have of course spent much more time on this than it really deserves. The primary purpose was to discover different ways of doing things and you have all helped a great deal.
I shall probably use SWITCH-CASE rather than a series of IF statements just to try something new.
I accept the mild rebuke concerning GOTO
Regards
Ed

Fasteddie:
I shall probably use SWITCH-CASE rather than a series of IF statements just to try something new.

Congratulations. You have chosen wisely. The switch/case statement is designed for this specific use (comparing a single integer variable against a large number of integer constants). It is easier to write and read than the equivalent if-else chain and is probably easier for the compiler to optimize.