PDA

View Full Version : Difference between Procedure and Function?



EmbranceII
29-01-2005, 01:13 AM
I wan to make a simple unit.
But what to use?
Procedures or Function?
What are the diferences?Wich is faster?And better too?

Chesso
29-01-2005, 01:41 AM
Dunno about speed but functions return a value or can i should say. Otherwise they are pretty much the same.

WILL
29-01-2005, 01:44 AM
Ok basically you need to know this:

:arrow: A procedure does NOT return a value. It just runs a block of code when called.

:arrow: A function runs a block of code when called, but also returns a value 'type' thats defined in it's declaration. Whatever is returned can be used in evaluations and assigned to a variable aswell.


Example of a simple procedure declaration:
procedure DoStuff(a: Integer);
begin
{your code goes in here...}
end;

Example of a simple function declaration:
function DoStuff(a: Integer) : Integer;
begin
Result := a + 5;
{after this block of code is all done whatever gets passed to result will be returned when it is done. CAn only be of the type that you defines above, in this example it's an Integer value}
end;

Example of the usage of the example function DoStuff:
{in an if statement}
if (DoStuff(5) > 12) then //
DoThings; // another procedure or funciton called DoThings

{assigning to a variable}
bob := DoThings(16);

{a bit more complex}
suzie := DoThings(56) + 7 * 12; // uses in a math function to get a value

With a Procedure you cannot use it this way. You can only call it. There is a way to pass back some values via the parameters, but thats a bit more involved and requires more explanation.

For more information about learning Pascal, I suggest either getting a good book on it that will serve as a nice reference later on when you have mastered the language OR search online. (google.com (http://www.google.com/) is great) There is even some new tutorials in the Library (http://www.pgd.netstarweb.com/library.php) that may help too, under 'Programming -> General/Advanced Programming'.

Hope it helps. :)

EmbranceII
29-01-2005, 10:53 AM
Yes it helped!;)
Thanks a lot!