Go Down

Topic: setup(void)? why? (Read 710 times) previous topic - next topic

otispunkmeyer

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

Code: [Select]
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.

JohnHoward

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.

otispunkmeyer

#2
Sep 04, 2013, 04:15 pm Last Edit: Sep 04, 2013, 04:21 pm by otispunkmeyer Reason: 1
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?

CrossRoads

void loop continues with it's next line after the interrupt occurs.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

MarkT


"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.
[ I won't respond to messages, use the forum please ]

otispunkmeyer


MichaelMeissner

#8
Sep 06, 2013, 01:34 pm Last Edit: Sep 06, 2013, 01:37 pm by MichaelMeissner Reason: 1

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

Code: [Select]
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:
Code: [Select]

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:
Code: [Select]
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):
Code: [Select]

extern int foo ();

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

Go Up