setup(void)? why?

I have been left with some Arduino sketches and in them, the previous owner has used:

void setup(void)

I understand that setup is a run-once function and that the void before hand denotes that the function will not be returning any values/variables. But what is the idea of also putting (void) in the brackets? I am not too familiar with arduino (I assume this is C/C++ or a variant of?), usual parlance is that whatever is in the brackets is the variable you are passing to the function from the main program. I don't see the point of putting void in brackets, but that maybe because I don't understand it.

Can anyone clear this up?

Thanks.

A void is nothingness. It indicates there is no value, no datatype, nothing. I would guess that setup() is equivalent to setup(void) as far as the code it generates, basically no values pushed onto the stack when the function is called. Explicitly stating 'void' is nice clarification/documentation of the fact there should be no value there.

I had a hunch that is what it might be, I guess it is just the neat and proper way of showing what is going on instead of putting void().

While I am here, the attachInterrupt things... I take it you can wire whatever electronics or sensors you have to put signals onto the interrupt pins (digital 2 and 3) and then when these are triggered, the interrupt forces the program to call a function or subroutine?

is that right? the syntax of

attachInterrupt(0,variable_1, RISING)
attachInterrupt(1,variable_2,RISING)

is when there is an interrupt signal on interrupt 0 (digi pin 2) call "variable_1" and do so when that interrupt signal is RISING. ?

following on

the function it calls is written:

void variable_1(){ some code}

so I guess this is, function variable_1, which will return nothing and is fed with nothing, will run "some code" when the interrupt triggers the call?

and finally, these subroutines or functions exist outside of the main program loop. So if it is brought out the loop to run the function due to the interrupt, does the program basically start the loop over or can it return to where it was?

void loop continues with it's next line after the interrupt occurs.

does the program basically start the loop over or can it return to where it was?

It doesn't start the loop over, it continues with the next machine instruction after the one that was interrupted.
Note that this isn't necessarily the next C statement or expression, but could be mid-expression, or even mid-variable access if the variable is longer than a single byte.

"could be mid-expression, or even mid-variable access "
Hence the reason you see sometimes see interrupts disabled before a critical calculation? Don't want the ISR code messing anything up.

CrossRoads:
"could be mid-expression, or even mid-variable access "
Hence the reason you see sometimes see interrupts disabled before a critical calculation? Don't want the ISR code messing anything up.

To clarify its only when you are doing something with variables the ISR is also accessing that you need
such a "critical section", otherwise the only effect of an interrupt is to cause a delay to the main program.

cheers chaps!!

otispunkmeyer:
I have been left with some Arduino sketches and in them, the previous owner has used:

void setup(void)

I understand that setup is a run-once function and that the void before hand denotes that the function will not be returning any values/variables. But what is the idea of also putting (void) in the brackets? I am not too familiar with arduino (I assume this is C/C++ or a variant of?), usual parlance is that whatever is in the brackets is the variable you are passing to the function from the main program. I don't see the point of putting void in brackets, but that maybe because I don't understand it.

Can anyone clear this up?

Thanks.

The first void means the function does not return a value. The second void means the function takes no arguments. Now, in C++ (but not C), it is perfectly acceptable to omit the second void:

extern void setup();
// ...
void setup ()
{
  // ...
}

The reason for having the void represent no argument is due to the original C standards process of which I was a member from about 1983 through the release of the standard as an ANSI standard in 1989 and reissued as an ISO standard in 1990.

In the original C language before the standards process, there were no prototypes to describe how the function would be called. If the user passed the wrong types or too many/too few, that was just too bad. One of the major changes to the C standards process was to import the notion of prototypes from the then new language C++ into C. However, C++ was a stricter language than C, and it required that a prototype for a function be in scope before you referenced it. So the problem was:

void setup();

already had a meaning in the original C language, i.e. you were declaring that the setup function returned nothing, but nothing was said about what arguments were passed. You could call setup with no arguments, one argument, etc. So in the standards committee, we added using void to mean that you were declaring the full prototype, and that the function setup took no arguments. Before it began its own standardization practice, the C++ language was changed to allow void to mean no arguments.

Now, you might protest that you haven't had to have a declaration in scope before making a call under the Arduino. That is because the Arduino IDE tries to be helpful, and it scans your code, and tries to add prototypes for each of the functions. For simple cases, it gets it right, but there are cases where the preprocessor gets it wrong.

If you are interested, the way you had a function in the original C language was (this is not legal to C++, which is used in the Arduino):

extern int foo ();

int foo (a)
    int a;
{
  return a+1;
}

Hello all,

I am not sure, if this is the right place to ask, so I apologise in advance!

Anyway, during my program I am returning to the setup () method, namely I have to run it at some point, however, I would like to ask how to pass a variable to this methods. For example, if I create a method within the program, like this:

void test(byte b){

// I can use this b
b+b;
Serial.println(b);
}

However, if I try this with a setup() it does not want to compile. For example, setup(byte b){...}

The main idea is to pass a certain variable from the ISR. For example, I would like to so signal what part of the setup() I would like to run... I would like to break the setup into two parts and run each accordingly !!!

Any ideas how to do this would be very helpful!

Best.

namely I have to run it at some point,

So put the code that you want to run in a separate function and call that when you need to, either setup() at the start of the program or from anywhere else that you need it.

Any ideas how to do this would be very helpful!

The signature of the setup() function is fixed. It takes no arguments, and returns no value.

You do not HAVE to pass values to functions. You can use global variables.

Thank you to both of you #10 and #11

So I guess I will just run a function from the code without even going into the setup, namely I will remove the code from the setup into a separate function and call that function at the beginning within a setup and then after with the interrupt, is this what you are referring to UKHeliBob?

I guess this as well applies to what PaulS is saying?

Best.

I will remove the code from the setup into a separate function and call that function at the beginning within a setup

You probably don't need to remove everything from setup(). Things like Serial.begin(), pinMode()s and other "once only" setup code could stay there, whilst initialisation of previously declared global variable might usefully be put in a function to be reused.