Board index » delphi » Need Help with general procedure!!!!!

Need Help with general procedure!!!!!

In a program containing two procedures:  The first asks for and reads in
the users input, and the second uses the input to solve the probelm.  How
can I use the value stored in a variable in the first procedure in the
second procedure.

I know it has something to do with local and global variable, but I'm not
sure how to begin.  I just started programming a couple months ago so I am
quite new at this.

Your help is much appreciated!

Thanks,

-Eric-

 

Re:Need Help with general procedure!!!!!


"William S. Mauskopf" <EricM...@aol.com> wrote:

Quote
>In a program containing two procedures:  The first asks for and reads in
>the users input, and the second uses the input to solve the probelm.  How
>can I use the value stored in a variable in the first procedure in the
>second procedure.

var
        input: integer;

procedure getinput(var input: integer);
procedure solveproblem(input: integer);

Re:Need Help with general procedure!!!!!


Quote
> In a program containing two procedures:  The first asks for and reads in
> the users input, and the second uses the input to solve the probelm.  How
> can I use the value stored in a variable in the first procedure in the
> second procedure.

> I know it has something to do with local and global variable, but I'm not
> sure how to begin.  I just started programming a couple months ago so I am
> quite new at this.

procedure Do_It (I : integer);
begin
  writeln (I*3)
end;
procedure Get_It;
var N : integer;
begin
  write ('Enter an integer value; I''ll process it: ')
  readln (N);                            { fetch user input }
  Do_It (N)                              { process the data }
end;

Re:Need Help with general procedure!!!!!


Quote
> "William S. Mauskopf" <EricM...@aol.com> wrote:

> >In a program containing two procedures:  The first asks for and reads in
> >the users input, and the second uses the input to solve the probelm.  How
> >can I use the value stored in a variable in the first procedure in the
> >second procedure.

> var
>         input: integer;

> procedure getinput(var input: integer);
> procedure solveproblem(input: integer);

Well, you confused me there, never mind the newbie.

Apart from the fact that input is a reserved word, I try to avoid using the
same variable name several times; sure, the compiler can resolve it by scoping,
but if you ever make a mistake, it can take ages to track down.

Answer:

this is a *very* brief answer, but is enough to get you going :

you can make a variable 'global' to both procedures. The 'scoping rules'
say that a variable is available to all code within the block in which
it is declared. If you declare it at program level, it is available to
both of your procedures e.g

program global_var;

var the_input : string;

procedure do_the_work;
begin
  WriteLn('The input was : ',the_input);
end;

procedure get_the_input;
begin
  Writeln('gimme some input ! ');
  ReadLn(the_input);
end;

begin  {program}
  get_the_input;
  do_the_work;
end.

better practise is generally to declare variables as locally as you can,
and passs them as parameters where required.   e.g

program use_param;

procedure do_the_work(work_param : string);
begin
  WriteLn('The input was : ',work_param);
end;

procedure get_the_input_and_do_the_work;
  var the_input : string;
begin
  Writeln('gimme some input ! ');
  ReadLn(the_input);
  do_the_work(the_input);
end;

begin  {program}
  get_the_input_and_do_the_work;
end.

Now, what you might also get in the way of replies is an admonition to
                   RTFM.
I wholeheartedly agree with that. Pay attention to the explanation of scoping
and of the various ways to pass parameters, which I have not touched upon
here.

I will agree that manuals sometimes don't have enough space to give
examples of everything, so I also recommend studying the code of others.
Before I post a question here, I look to the vast library of code which I
have amassed and do a string search through the lot for some stuff which
I think will show up as a keyword, or in a comment. As a habit, whenever
I see some code, I D/L it. It takes up next to no disk space and, even if I
don't think I'll ever need it, it's better to d/l than to wonder, months
later, where you saw something.

I hope that someome will take the time to post the URLs for the following.
Otherwise, do a search of the net, or watch this group. As soon as you have
one good page, it may have links to others & then it escalates.

try - http://www.shareware.com , http://www.jumbo.com,
http://www.cs.vu.nl/~jprins/tp.html  -- the Turbo Pascal Programmers Page
http://garbo.uwasa.fi/pc/
http://www.gdsoft.com/swag/swag.html - SWAG (Sourceware Archive Group)
url??                - Prof. Timo Salmi's Pascal Faq

hope this helps some,

gra...@pobox.com

Re:Need Help with general procedure!!!!!


Quote
Mike Copeland wrote:

> > In a program containing two procedures:  The first asks for and reads in
> > the users input, and the second uses the input to solve the probelm.  How
> > can I use the value stored in a variable in the first procedure in the
> > second procedure.

> > I know it has something to do with local and global variable, but I'm not
> > sure how to begin.  I just started programming a couple months ago so I am
> > quite new at this.

> procedure Do_It (I : integer);
> begin
>   writeln (I*3)
> end;
> procedure Get_It;
> var N : integer;
> begin
>   write ('Enter an integer value; I''ll process it: ')
>   readln (N);                            { fetch user input }
>   Do_It (N)                              { process the data }
> end;

What he's saying is you just call the one procedure from the other.
If you idn't want to do this, you need to declare your variable outside
the scope of each procedure (a "global" variable), then you can use it
in each procedure that needs it.  If you plan on modifying the variable
you need to declare it as Var in the procedure declaration:

procedure Change_Variable(var Variable_toChange: integer);

Other Threads