×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Pascal in the workplace FAQ

Procedure

How do i write procedures? by 5885885
Posted: 21 Feb 05 (Edited 22 Feb 05)

Procedures are extremely useful in Pascal programming, in any language.

A procedure is a sub section code that can be called by the main part of the program. This is useful as you can then use it again and again.It I like a small program all on its own, though the procedure name can not be the same
A very basic example of how to use one would be as follows:
Example procedure

CODE

    Program procedure_test;
    
    Procedure my_first_procedure;
Begin
    Writeln(æThis is a procedure.Æ);
End;
{Main program}
Begin
    my_first_procedure;
    readln;
end.

As you can see, the main part of the program calls the procedure by just entering in the name of the procedure.

When writing procedures you can have local variables for it, this is done by declaring them after procedure, but before program.
Local variables example

CODE

    procedure name;
    var
        localvariable : integer;
        laocalarray : array[1..10] of integer
    begin
        {some code}
    end;
You may want to note that these variables can be called anything you want, as long as there not global variables(variables declared at the beggining of the program). you also may want to noe that you ca reuse the names,
re-use of variables

CODE

procedure proc1;
var
  i : integer;
begin
   {some code}
end;

procedure proc2;
var
  i : integer;
begin
   {some other code}
end;

The other use of procedures is parameter passing. This means that when you call the procedure you enter in a variable, e.g
Parameter Passing

CODE

Program parameter_passing;
Var
    x, y, z : integer

Procedure add_together(num1, num2 : integer);
Begin
    z := num1 + num2;
end;
{main program}
begin
    writeln(æEnter in number 1 : æ);
    readln(x);
    writeln(æEnter number 2 : æ);
    readln(y);
    add(x, y);
    writeln(z);
end.

As you can see the parameters donÆt have to be called the same thing, they just have to be given variables.

Procedure can also call other procedures, but ONLY if they call a procedure above themselves in the code.
(this will however differ from compiler to compiler.)


Joey

I hope this helps, im not sure if it will, but oh wells, what harm is it doing. email me with any queries :
joey@sandz.plus.com

Back to Pascal in the workplace FAQ Index
Back to Pascal in the workplace Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close