Placing of setup instructions

I am working through the Arduino ShiftOut Tutorial. Looking at Code Sample 2.2 2 Byte One By One I notice the lines 107 and 108 are

pinMode(myClockPin, OUTPUT); pinMode(myDataPin, OUTPUT);

I would have expected to find these in setup() so there must be a reason that they are in a function.

What reason would that be?


I imagine that the author wanted to let people just copy/paste the function into another sketch and made sure that all the code would be there to function correctly.

You are right in thinking that the proper place for those two lines is within setup(), as they would be executed only once, before the loop(). Having them executed every time the function is called is wasted time, but causes no harm either.


Thanks jbellavance

Note too that there is a shiftOut() function as part of the Arduino core

It takes the data and clock pin numbers as parameters so for convenience it presumably sets their mode within the function.

it presumably sets their mode within the function

In the example code, the modes are set in the setup() section of the sketch. I don’t see any evidence that the function sets the modes of either pin.