Oberon Community Platform Forum
November 18, 2017, 03:31:12 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Use of unassigned local variable  (Read 6469 times)
hreba
Newbie
*
Posts: 14


« on: February 10, 2009, 03:56:45 PM »

The following code contains the same algorithm in a procedure and in a module body.
Code:
module Main;

var r, s: real;
    ok: boolean;

  procedure Inverse (a:real);
  var b: real;
      bo: boolean;
  begin
    bo:= true;
    do b:= 1.0 / a;
      on ZeroDivision,Overflow do bo:= false
    end;
    if bo then a:= a * b end;
  end Inverse;

begin
  write ("type an real#0 and RETURN: ");
  readln (r);
  ok:= true;
  do s:= 1.0 / r
    on ZeroDivision,Overflow do ok:= false
  end;
  if ok then r:= r * s end
end Main.
The compiler only considers only the procedure body as incorrect:
[14,24] [0383] Use of unassigned local variable 'b'.
My questions are:
  • Why are procedure and module bodies treated differently?
  • If there are cases where it is impossible for the compiler to decide whether a variable was assigned or not, why is this considered an error (and not just a warning)? The way the programmer would circumvent this situation is by placing a senseless assignment like b:=0.0 or b:=1.0 or whatever at the beginning, and others would ask themselves for the reason. The program becomes less readable this way. C# handles unassigned variables the same way, but this does not justify the case of Zonnon.
Logged
rmitin
Moderator
Newbie
*****
Posts: 22


« Reply #1 on: February 16, 2009, 09:59:11 AM »

The difference in module body is that global variables can be accessed by any other routine within the module and the use can't be checked only locally within the body. In contrast for local procedure variables it's always clear how they are used. I think compiler now issues a warning not an error, howerver error would be more suitable as this is a common source of errors.
Logged
hreba
Newbie
*
Posts: 14


« Reply #2 on: February 18, 2009, 01:54:55 PM »

Ok, I got the difference between procedure and module body. But even in the procedure body of the above example the compiler cannot know whether the execution reaches the variable and if it is unassigned in that case, because this depends on the value of a parameter unnknown at compile time. This is why in IMHO a warning would be more adequate than an error.
Logged
rmitin
Moderator
Newbie
*****
Posts: 22


« Reply #3 on: February 23, 2009, 09:15:10 AM »

The idea is that if you use variable at some place in the program then on every possible path that leads to this place (including exceptions) the variable must be assigned.
E.g. in the code:
Code:
procedure P;
var a: integer;
if condition then
  a := 1
end;
writeln(a)
end P;

The correct code would be

Code:
procedure P;
var a: integer;
a := 0;
if condition then
  a := 1
end;
writeln(a)
end P;

or

Code:
procedure P;
var a: integer;
if condition then
  a := 1
else
  a := 0;
end;
writeln(a)
end P;

Then we are sure that the output by writeln is always something that the programmer intended.

As an example the same rule is applied in C#.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!