[Pascal – TUT] Posts 7: Subroutine

1. Benefits of using subprograms(CTC)

– The program has multiple segments each segment perform a certain function{ then we used to do ctc segments on}
– In the program, There are sections need to go up, repeated many times in different places. To avoid having to rewrite the paragraph that people usually classified into several CTC program
– One other convenient use of CTC as one can easily check the correctness of it before linking to the main program. Thus the identification of errors and make adjustments in the program will facilitate more.
=> CTC is a program segment fulfillment or a certain function. In Turbo Pascal, have 2 CTC format: Functions and Procedures. Functions and procedures are the CTC, but other functions procedures that function returns a value to the call via the function name, the procedure was not

2. Declaring CTC

– Recalling the structure of a wit

	PROGRAM Tên_chương_trình; { Tên chương trình}
	USES ...; {Khai báo thư viện}
	CONST ...;{Khai báo hằng}
	TYPE ...;{Khai báo kiểu}
	VAR ...;{Khai báo biến}
	Khai báo CTC
	BEGIN {Chương trình chính}
		<các lệnh>;
	END.

Thus the declaration CTC located at the end of the declaration
the. Declarations and function calls
– Declare:

		FUNCTION <tên hàm>(Danh sách ác tham số):<Kiểu dữ liệu>;
		[Khai báo Const, Type, Var]
		BEGIN
			<các lệnh trong thân hàm>;
			<tên hàm>:=<Giá trị>;
		END;

Note always allow for the value assigned to the function name function returns a value when called
Example: the sum of 2 of x and y

	Function tong(x,y:integer):integer; {Do có giá trị trả về}
	var s:integer;
	begin
		s:=x+y;
		tong:=s;
	end;

– Function call

	<tên hàm>(danh sách các tham số thực);

Example:
tong(4,5);

b. Declarations and procedure calls
– Declare:

		PROCEDURE <tên thủ tục>(Danh sách các tham số);{không có giá trị trả về}
		[Khai báo Const, Type, Var]
		BEGIN
			<các câu lệnh>;
		END;

Example:

	Procedure inso(n:integer);
	var i:inteher;
	Begin
		for i:=1 to n do
			write(i:5);
	end;

– Procedure call

	<tên thủ tục>(danh sách các tham số thực);

Example:
inso(6);

3. Global variables and local variables

– Global variables are variables declared in the main program. These variables can be used anywhere in the program and exist for the duration of the work program
– Local variables (local variables) the variables are declared in the CTC. These variables are only used within ctc which it is declared. After the ctc form variables will no longer exist.
Example:

PROGRAM vidu;
Var a,b,c:integer; {3 biến toàn cục}
PROCEDURE thutuc(n:integer);{n là biến cục bộ}
var i:integer; {i là biến cục bộ}
begin
	for i:=1 to 10 do writeln(i);
end;
BEGIN
	a:=5;b:=6;c:=8;
	thutuc( a);
	thutuc( b);
	thutuc( c);
	readln;
END.

– During the meeting the local variable with the same name as a global variable is not to be confused machines that will perform on local variables. Global variables are not affected.

4. How to pass arguments to the subroutine

– CTC does not require parameters (later named ctc) if you do not use them directly or using global variables
– When you pass parameters in call parameters ctc to the order and type corresponding to the declaration ctc.
Example:

Procedure inso(a:integer; ch:char);
begin
	{các lệnh của CTC}
end;
{gọi}
inso(13,'a'); {lời gọi đúng}
inso('a',13); {loi goi sai}
inso(13);{lời gọi sai}

– Parameters form (to) parameters after the function name and the declaration procedure.
– The parameter is the name of the function the following parameters and procedures in the call.
– Parameters: parameters are declared after the var keyword. The real argument is the variable is not worth. Parameters can be changed in the CTC and after keeping it out of the CTC value changes.
– Join value: parameters without notice notch behind the var keyword. These parameters can be real values, constant, variable. Join value may change in ctc ctc but after the end of its return value as the original.
– The parameters of the function is the participation of, the parameters of the procedure can be passed by value or parameter.

5. Distinguishing using functions and procedures

Ham other procedures that function returns a value to the call via the function name, the procedure was not.

*Use function
– Results of the problem returns 1 unique value (scalar type, string or pointer type).
– A call to CTC in the calculation expression.

*Use the procedure
– Results of the problem does not return a value or return value or returns the data type structure (Array, Record, File)
– The call CTC is not in the calculation expression.

Attention: If a job can be done by the function will certainly done by procedure {However, more complex functions using} but a program that made it unlikely procedure was done by function.

For Borland Pascal 7.0 we can call such a procedure call. Not necessarily get the return value. To achieve this in the Options menu >Compiler declaration syntax to expand (Extended syntax), or in the program should be guided translation {$ X }. Unless, when compiling (type F9) Pascal will be the error message "Error 122: Invalid variable reference”.

6. As of recursive subroutine

A CTC in Pascal can call itself. A so-called call a recursive call

Consider the following example:
Enter 1 number n and n!
I know n! = 1 if n = 0 in case n>N = 1 we have!=n.(A-1)!
Of functions n!

function giai_thua(n:integer):longint;
begin
	if n=0 then giai_thua:=1
	else giai_thua:=n*giai_thua(n-1);
end;

– Noted:
+ When using recursive conditional ending recursive (TH degenerate). In such conditions we consider ending recursion is n = 0. If there are no conditions for the end of this program we will repeat indefinitely.
+ Always have recursive call, of TH on the call giai_thua(A-1);


Original article: vietsource.net