Request help: Functions in plain English
Author Message
Request help: Functions in plain English

Hi. I am studying Pascal and have come to the stage of using
functions. I am having some difficulty understanding how they work.
Particularly parameters and passing values into a function (using
brackets after the function name for example). Would anyone please
give me a simple answer to how they work?
Thanks,
Mike

Sun, 09 Oct 2005 06:06:14 GMT
Request help: Functions in plain English
Dear Mike,

A function is similar to a procedure, but it has a return value

For example:

Procedure sum(a,b:real; var c:real);
Begin
c:=a+b;
End;

This procedure could be written in the form of a function as follows:

Function sum(a,b:Real): Real;
Begin
sum:=a+b;
End;

These two pascal constructs are similar. The first one uses a
procedure call and uses a var to return the sum to the caller.
The second one uses the return result as the sum value.

Regards,
Mike McWhinney
elja, Inc.

Quote:

>Hi. I am studying pascal and have come to the stage of using
>functions. I am having some difficulty understanding how they work.
>Particularly parameters and passing values into a function (using
>brackets after the function name for example). Would anyone please
>give me a simple answer to how they work?
>Thanks,
>Mike

Sun, 09 Oct 2005 12:12:12 GMT
Request help: Functions in plain English

at Tue, 22 Apr 2003 15:06:14 :-

Quote:
>Hi. I am studying pascal and have come to the stage of using
>functions. I am having some difficulty understanding how they work.
>Particularly parameters and passing values into a function (using
>brackets after the function name for example). Would anyone please
>give me a simple answer to how they work?

They work exactly as for procedures, so I assume that you don't
understand those either.

The only differences are :
What they are called, function/procedure.
Before leaving a function, a value needs to be assigned to the
function name; in Delphi, the word Result can be used for this
name.
A function generally does not change its parameters; but it can
do so; a procedure generally does change at least one
parameter; but it does not have to.

The declaration and call provide a positional mapping between the names
used in the declaration and those used in the call.

In TP/BP, there are three sorts of parameters :
Value (spelt "") : a copy of the external parameter is used,
and can be changed, internally; the original cannot be changed
directly.
Variable (spelt "var") : the original parameter is used, and can
be changed, internally.
Constant (spelt "const") : the original parameter is used, and
*cannot* be changed, internally.

For var and const, the address if the parameter is actually passed.

Constant combines the safety of Value with the efficiency of Variable.

Sqrt is NOT actually defined like this; but it could be :

function Sqrt(const X : extended) : extended ;
begin Sqrt := exp(0.5*ln(X)) end ; {untested}

--

<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt>  RAH Prins : c.l.p.b. mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

Sun, 09 Oct 2005 21:58:43 GMT
Request help: Functions in plain English
--------------------------------------------------------------------------------

Thanks for the help guys. I've just about cracked it now anyway. Last
night I spent some time debugging something i'd written a few weeks
ago and it worked pretty well.

Mike

Tue, 11 Oct 2005 05:44:08 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages