Sorry, but you clearly don’t understand what forward declaration is. It means you HAVE to define your variables BEFORE the code — ANY of the code — in your function block.

See Pascal or Ada.

procedure test;
var
a : integer;
b : string[240];
begin
{ do something with a and b }
end;

“begin” and “end’ being the equivalent of {} for the function. You CANNOT declare new variables anyplace inside begin/end.

It would be like if in C you had to:

function()
int a;
*char b;
{
// do something with a and b
}

… and it was genuinely IMPOSSIBLE to create new variables inside the {}. With mandatory forward declaration you CANNOT just make a variable anyplace you choose.

Hence Pascal/Modula/Ada has zero equivalent to this:

for (var i = 0; i < 20; i++)

Because declaring a new variable at that point in the code is impossible. Once you “begin” — again the equivalent of “{“ — creating a new code block and start having logic, creating variables is COMPLETELY OFF THE TABLE! It is not allowed!!! Globals have to be declared outside your main loop “begin/end.” and function variables must be declared between the procedure/function declaration and your “begin/end;” block.

Basically imagine never being allowed to declare variables inside any {} ENclosure. Instead you have to declare all your variables and their types at the start after your procedure/function definition and before your opening {

THAT is forward declaration, and C and C syntax languages lack it! That’s why C has to do multi-pass compilation and why JavaScript has hoisting to fake it.

And it’s part and parcel of why C is often a bug inducing train wreck where one typo can just magically become a new variable in places where Wirth family languages wouldn’t even let you compile the code.

Also why if JavaScript had it, we’d not need this hoisting crap, this new and pointless let/const rubbish, and people would make a lot less mistakes in accidentally using or re-using variables since it inherently gives you a reference of what’s in use. It also results in cleaner memory handling since you can just allocate local variable space on the stack from the get-go with a uniform release mechanism!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store