I am confused aout how it uses C++

Hello, I apologize if this is not an appropriate post.

I just learned C++ in college, so when I decided to play with the arduino that I received for my birthday, I was surprised to see that the code LOOKS like C++, but upon further inspection of the code in my projects, I became confused about the programming of this device.

I thought at first that void loop (), void setup (), and the other voids were functions. But then I realized that these functions have no declaration in my source codes, and they have no definitions in my source codes. I thought that these were being defined within the source codes that I have, but that is impossible. void loop just starts looping what ever I put into it. Well, functions don't do that without a loop in their definitions.

I am GUESSING that everything that I write in the IDE is copied into a main class, and the ide just automatically preprocesses the parts necessary for the program to work before being uploaded into the arduino.

Besides that stuff, I have an actual problem with the abstraction of a lot of this data.

Part of my source code uses this:

/*
I take it this is ASCII, and I guess this is an array? I deleted some letters if this is ASCII, thinking that I could get rid of the letters that display on my 4-digit 7-segment display. Once I did that, it displayed nonsense. So I put it back to how it was.
*/
unsigned char table=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c
,0x39,0x5e,0x79,0x71,0x00};

after the void setup function, which I don't understand because everything about it is hidden away in some other file except what is being put into it, I have this stuff:

/*
Where did the unsigned char num come from? It looks like maybe it takes a value from that thing I thought was an array, but I don't get what it's even doing lol everything's hidden.
*/
void Display(unsigned char num)
{

digitalWrite(latch,LOW);
shiftOut(data,clock,MSBFIRST,table[num]);
digitalWrite(latch,HIGH);

}

/*
Besides the fact that I don't get what kind of loop this is actually using, I REALLY don't understand what these numerical values are that are being sent to "Display". I thought that these were corresponding with the values in the "unsigned char table". Well that table has 17 values, and this is only using 15. 1 is definitely 1, and 2 is definitely 2. But the numbers only go up to 0 (which would be ten with the absence of a place for the second digit). Numbers 11 through 15 are letters that are being displayed. Where are they coming from? Where is "Display" at? I SEE that "Display" is typed as a funtion, but I can't see what it is doing. So where is it actually at? What is determining the segments to display the numbers or letters that I want?
*/
void loop() {
Display(1);
delay(500);
Display(2);
delay(500);
Display(3);
delay(500);
Display(4);
delay(500);
Display(5);
delay(500);
Display(6);
delay(500);
Display(7);
delay(500);
Display(8);
delay(500);
Display(9);
delay(500);
Display(10);
delay(500);
Display(11);
delay(500);
Display(12);
delay(500);
Display(13);
delay(500);
Display(14);
delay(500);
Display(15);
delay(500);
}

Display is a function. unsigned char num is a parameter to that function.
When you call Display(1), num becomes 1 for that function invocation.

Arduino is programmed in C++, setup() and loop() are defined within the Arduino libraries, as is main(). Look into the source code that comes with the IDE :slight_smile:

nastybuttler322:
I just learned C++ in college,

That contradicts

nastybuttler322:
I thought at first that void loop (), void setup (), and the other voids were functions.

because nobody that knows C(++) would call them ‘voids’.

In the Arduino context setup and loop (and some other functions) are called by main()

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

nastybuttler322:
I am GUESSING that everything that I write in the IDE is copied into a main class,

There is no main class in C(++), you are mixing Java and C.

I don’t answer questions about parts of code, or code that uses delays to control timing.

The array is probably hex that would make more sense as binary and correlates to the 7 or 8 segments on/off in a particular order.
0x06 would be “1”, 0x07 being “7” makes sense as it’s just one more segment lit up than a “1”.

@nastybuttler322 is currently enjoying a brief forum timeout for an astonishingly tiresome and ill-informed post, now deleted.

It is C++. Look at e.g. the overriding of Serial.print(); it can take different types of arguments like int, char* etc as well as variable number of arguments.

The IDE tries to be intelligent and will e.g. attempt to generate function prototypes; good for newbies but it doesn’t teach people to use C/C++ properly.

You can encounter situations where the IDE does not manage to get it right.