On Tue, 12 Dec 95 21:28:09 GMT,
Alexander Noble <a...@elbon.demon.co.uk> wrote:
Quote
>I've been given a task to do in TP7 but would be very grateful
>if somebody could clarify a couple of points for me... be gentle,
>I'm a programming newbie...
>It goes like this....
>"Write a menu-driven program which will convert sterling to a choice
>of any 4 foreign currencies with the appropriate exchange rate being passed
>as a parameter into a single procedure which can perform all conversions
>and pass back out the exchanged amount."
>I don't want anybody to write this for me but I just want to know
>why I would want to pass the exchange rates into the proc as parameters
>instead of just having variables or constants which are global to both
>sub-programs holding the ex rates? The exrates will have to be stored
>somewhere so they might as well be used by the proc as they stand?
>Thanks in advance, s'cuse any non-programmerisms, I'm only learning :)
Using global variables for this is okay if you only have to call it in
one place, but parameters are cleaner and easier to understand if you
have lots of calls..Contrast:
procedure Convert; begin Gout := GInp * GRate; end;
GInp := 123.456; GRate := 1.123;Conv; writeln('result1=', Gout );
GInp := 344.555; GRate := 4.567;Conv; writeln('result2=', Gout );
GInp := 567.533; GRate := 9.221;Conv; writeln('result3=', Gout );
--------------
function Convert( Ginp, GRate: real ): real;
begin Convert := GInp * GRate; end;
writeln('Result1 = ', Convert( 123.456, 1.123 ) );
writeln('Result2 = ', Convert( 344.555, 4.567 ) );
writeln('Result3 = ', Convert( 567.533, 9.221 ) );
Also, in complex programs, with either recursion, or multi-tasking
going on, you might have several calls to Convert going on at the
same time. The global variables would then step all over each others
values.
In larger programs, you might have dozens of procedures, and keeping
track of what global variables are in use at any point can get too
confusing. using parameters and local variables can help clear up
this confusion.
So in general, parameters are a GOOD THING. The program runs better
and looks better too. Well worth the slight extra initial effort.
-----------------------------------------------------------------------
George R. Gonzalez g...@boombox.micro.umn.edu
Sr. Sys. Programmer University of Minnesota