Activity : Local and Global variables
Constants and variables are either ...
  • GLOBAL  (they can be used anywhere in the program)
  • LOCAL (they can only be used inside a subroutine)

Here is a (pseudocode) program. Identify whether each of the variables is GLOBAL or LOCAL.

 

program Nonsense;
  var alf, bert : integer;
     
function fred : real;
  var jim : real;
  var ian : integer;
  begin  
    jim = 2.5;
    ian = 10;
    fred = alf * jim + ian * bert
  end;  
     
begin {main program}
  input(alf);
  input(bert);
  if alf > bert then 
    output(fred)
  else  
    output("Goodbye");
end.    
This is a nonsense algorithm that does nothing sensible!
alf (Global or Local?)
jim (Global or Local?)
bert (Global or Local?)
ian (Global or Local?)
...and the name of the function is...

   Back