From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: [Beginner Problem] Varibles not getting assigned correctly
Date: Fri, 11 Jan 2008 19:00:15 -0800
Date: 2008-01-11T19:00:15-08:00 [thread overview]
Message-ID: <Ze6dnSP4wpUDsRXanZ2dnUVZ_jednZ2d@comcast.com> (raw)
In-Reply-To: 13oe10uh39g0v85@corp.supernews.com
"Shane Smith" <rusty_@users.sourceforge.net> wrote in message
news:13oe10uh39g0v85@corp.supernews.com...
> Hi all,
>
> I'm trying to write a simple program (that manacla game), I've
> encountered a problem where the variables that are used in a nested
> procedure can't be assigned a value. I'm using GNAT 4.1.2 on Linux.
>
Hi.
Since you volunteered "beginner" problem I would like to make a suggestion
on program structure.
When I was in college (a couple of decades ago) where I learned Pascal, one
of my instructors had the requirement that no global variables were
permitted. Only global types.
If you are want to avoid using multiple packages (until you're a little...
very little) farther along you can structure the main procedure:
procedure Manacala is
procedure A( ... args ... ) is
begin
...
end A;
procedure Main is
... variables delclared here ..
begin
... code here that passes arguments to other procedures/functions
end Main;
begin
Main;
end Manacala;
When programs are broken down this way you can understand the operation of a
procedure or function based on just the arguments and the type definitions.
This makes it easy to understand small pieces of code and how they interact
with data.
Regards,
Steve
(The Duck)
> ---CODE---
>
> procedure Manacla is
>
> type pits is array (1 .. 14) of Integer;
> type Score_Type is array (1 .. 2) of integer;
> type arr_point is access pits;
> board : arr_point;
>
> Score1 : Integer := 0;
> Score2 : Integer := 0;
> Scores : Score_type := (0,0);
> Player : Integer range 1 .. 2 := 1;
> Winner : integer := 0;
> Test : constant integer := 11;
> Pit : Integer := 0;
>
> ...
>
> procedure Status (Player : in integer)
> is
>
> begin
> -- Check if 'playing pits' are empty, if not exit
> while board.all(2 .. 7) = (0,0,0,0,0,0) or board.all(9 .. 14) =
> (0,0,0,0,0,0) loop
>
> if Player = 1 then
> if board.all(2 .. 7) = (0,0,0,0,0,0) then
> Winner := 1;
> else exit;
> end if;
> else
> if board.all(9 .. 14) = (0,0,0,0,0,0) then
> Winner := 2;
> else exit;
> end if;
> end if;
> end loop;
> end Status;
>
> ...
>
> procedure Player1
> is
> Pit : Integer := 0;
> Player : Integer := 1;
>
> begin
> ...
> Status(Player);
> end PLayer1;
>
> begin
> ...
> player1;
> end Manacla;
>
> ---END CODE---
>
> Basically main body calls player1 and player1 calls status(player) (which
> edits the winner variable).
>
> In gdb 'p winner' gives "$14 = 7270388".
> It happens to arrays and access varibles as well.
prev parent reply other threads:[~2008-01-12 3:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-11 5:52 [Beginner Problem] Varibles not getting assigned correctly Shane Smith
2008-01-11 8:01 ` Niklas Holsti
2008-01-11 10:46 ` Peter Hermann
2008-01-11 17:51 ` Jeffrey R. Carter
2008-01-12 3:00 ` Steve [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox