Board index » delphi » Why use parameter passing and not global variables?

Why use parameter passing and not global variables?

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 :)

--
< Alexander Noble >.........< a...@elbon.demon.co.uk >...........< Cheers! >
                                 Never apologize, never explain...     HST

 

Re:Why use parameter passing and not global variables?


In article <818803689...@elbon.demon.co.uk>
           a...@elbon.demon.co.uk "Alexander Noble" writes:

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 :)

Anyway, they are trying to teach you to write nicely packaged code.  If you
have a function to convert currencies that is self-contained then you can just
drop it into another program and it will work.
Also, global variables can be {*word*193} as you forget when certain vars are being
used, making you run out of nice 'for' variables.

What exact language are you doing this in? It's just the menu bit seems the
hardest section whilst the conversion-in-a-fucntion bit is a doddle.

--
* TQ 1.0 * The 'Just So Quotes'.
"The other major kind of computer is the "Apple," which I do not recommend,
because it is a wuss-o-rama new-age computer that you basically just plug in
and use. This means you don't get to participate in the most entertaining
aspect of computer owning, which is trying to get the computer to work."
- Dave Barry 2/6/94

Re:Why use parameter passing and not global variables?


Ok, thanks for the help everybody, its appreciated, better go and
get those parameters in order...

Tom; The course I'm following is kind of modular, introducing a few
new statements and concepts at a time, I've just got onto procedures
but the menu bit was completed a couple of weeks back -though I've
just written my own proc to draw a configurable border around a window!
(I am very proud! its all parameter-based too, everybody'll be glad to
hear!=) which has made it look nicer, I'm using TP7 as far as I know.

Quote
In article <4alm08$...@news.2sprint.net> ggr...@2sprint.net "Valtar" writes:
>Also, if you are wanting more practice and learn more, follow the
>tutorial I've been writing.  It's in the basics right now, it will
>cover everything, but soon, it will get VERY VERY advanced and
>interesting. :>

Where can I find it? if any bits of it are released yet?

Thanks again..

--
< Alexander Noble >.........< a...@elbon.demon.co.uk >...........< Cheers! >
                                 Never apologize, never explain...     HST

Re:Why use parameter passing and not global variables?


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

Re:Why use parameter passing and not global variables?


Quote
Alexander Noble <a...@elbon.demon.co.uk> wrote:
>>Also, if you are wanting more practice and learn more, follow the
>>tutorial I've been writing.  It's in the basics right now, it will
>>cover everything, but soon, it will get VERY VERY advanced and
>>interesting. :>
>Where can I find it? if any bits of it are released yet?

Part 1 and 2 has been released so far into this very newsgroup.
If they're not visible to you, I will e-mail them to you.  Then just
look in here for future releases of parts (already got 3 written --
got a neat little game in there for practice programming . :>)

Other Threads