3 ms·
apart from that (which i agree with) is that in most pascal-like languages you have to define variables at the top of a function, and their scope is the whole f
by zabzonk 4y ago
apart from that (which i agree with) is that in most pascal-like languages you have to define variables at the top of a function, and their scope is the whole function. both c (nowadays, define at top of a {} scope) and c++ (define anywhere inside a function) do much better which, together with irritating long names, may explain why c and c++ won out over the pascal-likes.
speaking as an ex professional delphi programmer here - i have written lots of object pascal code, and have no desire to write any more.
- unwind 4y agoC is declare anywhere and has been for a looong time (C99).
- zabzonk 4y agosorry, my bad, i don't do any c programming these days. but to be pedantic, i think you mean define rather than declare, though of course you can also declare things at different scopes.
- stefncb 4y agoNo, I think they do mean declare. In C89 you can define a variable anywhere, like so: int x; f(); x=3; The improvement was allowing declarations anywhere.
- rightbyte 4y agoIsn't the first row both the declaration and definition is this case? (I assume f() is a call.)
- Jtsummers 4y agoC (and C++) considers the declaration to be separate from the definition, though you can combine them. The declaration is the name + type, definition provides a value. The example code is meant to illustrate separate declaration and definition with some code (the call to `f`) between the two. If you try to use `x` before it is defined (the assignment) your compiler should give you a warning, at least. If it doesn't, find a new compiler.
- LegionMammal978 4y agoFor block-scope declarations, it's not the definition but the initialization that you're thinking of. At least in C, every block-scope variable declaration is a definition: > A definition of an identifier is a declaration for that identifier that: > — for an object, causes storage to be reserved for that object; [...] But initialization (in C) does not occur for block-scope objects without the optional initializer.
- stefncb 4y agoOkay, that's somewhat confusing. So a declaration is when the identifier enters the scope, and a definition is for a variable specifically. So a declaration that's not a definition could be a forward declaration for a function, or a typedef.
- LegionMammal978 4y agoYes, or the declaration of an extern variable.
- pjmlp 4y agoDelphi supports local variable declarations for quite some years now, and Ada does it since its introduction in 1983.