Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i write procedures?

Procedure

How do i write procedures?

by  5885885  Posted    (Edited  )
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:
[blue]Example procedure[/blue]
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.
[blue]Local variables example[/blue]
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,
[blue]re-use of variables[/blue]
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
[blue]Parameter Passing[/blue]
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top