declaring variables

Hi, I am new to arduino programming. what is the idea of declaring variables? in the basic programming language you never had to declare variables. you just said "let x=5" or something more complicated like a mathematical equation.
Is it something to do with compiling into machine code? making it easier to compile perhaps?
I was bought up on basic which was the original programming language and it always worked well for me on the Sinclair machines. I could do anything.
It would be a big improvement if you could do away with declaring variables in arduino. It would make the programming much easier.

You define the data type in the declaration/definition of the variable.
If you didn't, you'd have to have naming conventions, like in BASIC (x%, x, x$ etc)

Okay.

Suppose I do say, "x=5". Is x a byte, float, int, long, signed or unsigned data type?
How would I keep track of all my variables? "x=5" would be buried somewhere in hundreds of lines of code.

"x=5" would be buried somewhere in hundreds of lines of code.

As could

byte x = 5;

and, of course, somewhere else in a different scope there could be

int x = 5000;

to say nothing of the possibility of there being a local and a global variable of the same name and not knowing which is being used

I was bought up on basic which was the original programming language

FORTRAN, COBOL, Algol and even Lisp predate BASIC.

TheMemberFormerlyKnownAsAWOL:
FORTRAN, COBOL, Algol and even Lisp predate BASIC.

I think maybe assembly language or machine code pre-date all of them!

Most people who think they are programming a computer are just programming another program but don't know the difference.

PerryBebbington:
I think maybe assembly language or machine code pre-date all of them!

I know they do, but they're not high-level languages.

petercl14:
It would be a big improvement if you could do away with declaring variables in arduino. It would make the programming much easier.

Arduino is programmed in C++ and probably always will be. So no chance of your wish ever coming true. There are variants that are coded in MicroPython that might suit you better though.

petercl14:
It would be a big improvement if you could do away with declaring variables in arduino. It would make the programming much easier.

Once you get used to it, you'll learn to appreciate a strict type system. Even the most popular modern dynamic languages like JavaScript and Python are moving towards more extensive type systems, using TypeScript and type hints.

Type-less programs are simply not scalable, typing results in clearer library APIs, better static analysis, code completion, etc.

Microcontrollers have limited memory and processing power, so processing type information at runtime would be a silly idea. Strict static (compile-time) type systems also catch mistakes much earlier, constantly debugging type errors at runtime on an embedded device would be really cumbersome.


That being said, static type systems don't necessarily require you to spell out the complete type each time, in C++, you can use the auto keyword, it asks the compiler to figure out what type a variable is. For example:

auto x = 5; // type of x is `int`
auto f = 3.14f; // type of f is `float`
auto u = 10000ul; // type of u is `unsigned long`
auto &s = Serial; // type of s is `HardwareSerial &` (on an UNO)
// auto is especially useful for longer type names:
std::vector<Servo> v;
auto it = v.begin(); // type of it is `std::vector<Servo>::iterator`

Pieter