Question

In a exam in my course of Language Programming was this question:

Is the next program valid in a imperative programming?

   int a = 0;
   z = a + 2;
   int z = 3;

was not specified or grammar, or syntax, or anything extra. My answer was YES because eventually there may be a language, a syntax and a grammar design for wich is valid use z without declare it and 're-declarate' z later. My grade was 0.0 but I still find it unfair. could help to clarify the issue?. Thank.

No correct solution

OTHER TIPS

You were marked unfairly.

Paste the following code snippet into the debugger console of your browser:

(function() {
    var a = 0;
    z = a + 2;
    alert(z);
    var z = 3;
    alert(z);
})()

Considering your course is titled "Language Programming", I imagine "imperative programming" has to be interpreted strictly. Quoting the current tag-wiki for , we have

Imperative programming is a paradigm of expressing the logic of a computer program or computation by explicitly describing its control flow in terms of statements that change a program state.

Arguably the most reasonable interpretation of the given C-like code is that int z = 3; is a variable declaration (and assignment), and the line z = a + 2; is thus usage before declaration. Assuming this is a legal program, this does not follow the imperative paradigm since its control flow was not explicitly described i.e., it wasn't sequential.


That said, you are right that since there was no specified grammar or syntax, the program could literally do anything, including what you specified, calculate π, or display a pink elephant on the monitor. Obviously, we need to draw a line somewhere. Given that your course is titled "Language Programming," I think that critically evaluating how reasonable your specification for a possible language is a good way to go.

We have plenty of languages where simple use of a previously unknown variable implicitly declares it, so that's quite reasonable. However, the "re-declaration" part of it, I find quite unusual and confusing and believe that it would be a bad language feature. I'm unaware of any major programming language which allows it1.

Therefore, I believe that your answer of "YES" in that narrow circumstance should at best have been a side note in an answer where the main answer was "NO".


  1. Similar, but unrelated cases, are where different variables have the same name but different scopes, and where the same variable can be assigned objects of different type.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top