[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
I am writing 1 quiz show Quiz pascal. If you choose not wrong, the program will call the procedure with the statement write(‘Sai roi ! What a pity.');…. If true, the opposite. Let me ask if in doing so it is what allows procedure call sir ?
I just write and call procedures.
Example procedures are thongbao TEEB when called also called thongbao() rs only
Sensei, e also get involved want to do programs like quizzes v, that the DAG secret e do not know where to start, Mr. hint e sir little PES, e thank you much sir
This one command if else is.
Indeed, e still can not imagine that it ntn DC; that f want it repeated, use case….of Dug ko sir
I want to use the repeat loop.
Can you provide specific guidance for e PES sir, e tangle all sir rùi, hope he help; e thank you sir
Using loops to repeat the question. In each question make the show questions and answers selected people, that's all.
Sensei, e also get involved want to do programs like quizzes v, that the DAG secret e do not know where to start, Mr. hint e sir little PES, e thank you sir
mk is a bit long rùi new class 8 should deny that he will communicate to amenities : he should use if function (condition) then (statements 1) else (statements 2);
given đk true then execute the statement 1 wrong then execute the statement 2;
I do not understand the parameters and parameter forms it locally different varieties and how he ạ. Not very clear in writing textbooks. According to the teachers they do not understand the true ntn:
CEO:
var x: integer;
procedures vidu1( x: integer) ;
………. {code}
then x is considered a parameter because it is locally in the declaration private var, Its name is used directly in CT
CEO:
var x:integer;
procedures vidu2( and: integer);
…… {code}
start
write('Nhap x: '); readln(x);
vidu2(x);
then y is called the formal parameters in the declaration for the procedure, but it represents only a form instead of the other values in the body of the procedure program vidu2
Desiring to help teachers explain
You understand how to be true then.
I'm writing a program that uses the function when it is not the sum function or an expression that is to check the condition that all required subject at least one function, how ạ?
for example, it starts to print a list of students with their condition and what middle name is a name that I can function and how sir ntn?
This alone does not quite understand your description.
I'm just new to pascal
I found some very useful article
Development should continue to blog offline
I will regularly monitored to learn more knowledge from blogs sir
Sensei, Authors Ho Si Dam, Hu Ha, Do Hung Tran, Nguyen Duc Nghia, Thanh Tung Nguyen, Ngo Anh Tuyet said that local variable do not contain formal parameters:
var tich: real;
function Luythua(x: real; to: integer): real; {x, k is the parameter form}
where J: integer; {j is a local variable}
start
tich:=1;
for j:=1 to k do tich:= Tich * x;
Luythua:= tich;
than;
start
—–
than.
Informatics textbook reading class 11 (Page 92, 93, 94) then read all of Teacher, I wonder: Không biết theo bên nào?
I have not quite understood your question.
Skin…
I have neglected and not very interested tubol pascal and children are suffering the consequences sir…? basic knowledge almost lost entirely, pt writing to you now as a luxury
I realized the seriousness of the harm through their exams… now I really want to start from scratch sir
Please can you just what you should do first is not it?( reviews of children is fundamental to start from somewhere sir?). Skin, sir please help teacher( yes, please annotations added that this year the grade 11 and I want to reach the end point in the semester exams … water jump to new leg is wrong, I know, but that dance is more than stand still). Look forward to your feedback soon
Thank you
Hello Mai, really sorry for reading your comments a bit late. This time you did not know semester exam or have their learning oriented programming pascal but not expecting positive results.
About how you should ask, What first study to achieve a high score, you look back to the textbook basics. Read it and if the place does not understand can ask teachers or friends can send their mail. Then can refer to online (eg his blog about the basic program). So is best for your exam.
But if you want better programming, try to learn more, to think what we want to do (such as Snake game or a program that something), and gradually learn to do it. Just like that you will well up.
If you have any questions please email yourselves.
Wish you good health and learning.
Teachers help children write subroutine direction to enter 1 strings and give 1 string obtained from the string after removing spaces k sir? Cô cho bài tập mà e k bt hướng làm 🙁
Gợi ý là dùng vòng lặp for, kiểm tra cái nào là dấu cách thì xóa nó đi. 🙂
Var x,and: Integer;
Procedure TT(the,b:Integer);
Var x,and: Integer;
Begin
x:=3; and:= 4;
End;
Begin
x:=5; and:=6;
TT(x,and);
Write(x,and);
End.
thanks to help teachers explain KQ
Procedures TT with 2 variable x, as above, it is passed by value, so after getting off the procedure, the variables will not hold value if the procedures have instead poured. Thus resulting in the x and y remain 5 and 6
Where x:Integer;
Procedure TINH;
Where x:Integer;
Begin
x:=1; x:=x+12;
End;
Begin x:=10; THE PROVINCIAL; Writeln(x); End.
This question again and KQ? why to declare variables in the KQ ctc back = 10
When turning in the subroutine name with global variables, the subroutine uses only its variable that does not do anything to global variables. Means that global variables will not be any impact in the subroutine.
Instead oi lam e can gap a write programs collect the network entry 10 integers and print imager
E cam on
Teachers can help children pascal Programming using subroutine for calculation of a corner any message , How do sir???
Kids are very urgently needed, Please help me vs
dear teacher you need to write a program that calculates S:=1!+2!+3!+…n! using subprograms. please teacher help
You need help on problems? Making, or an error,… ???
Evening way sir
You can write 1 subroutine to compute x!, then write 1 subroutine sum of factorial based on the x! has calculated.
Various command functions and procedure at any point, huh teacher?
You read items 5 in the article stating I got you.
Dear teacher you want to declare a string type in the CTC must declare how?
You just declare it as normal, others not at all.
Thầy ơi cho em hỏi, return value here is that what was?.
Ie after calculation done something, functions which return results.
Sensei, e need to write 1 ct that while running it will 1 picker, Meanwhile, if you press 1 certain numbers on the keypad, it will appear 1 ct solutions or answers. VD picker here is the area of the image. if dialing 1 it will calculate the area 1 certain image, ie like this:
Calculate the area of the image
0. exit
1. triangular
2. rectangle
3. square
4. circle
if you press the number 4 it will calculate the area of a circle, Similar to others
hope he help, I have tried many ways but none are both (but now I'm in Grade 8, I have not experienced).
Thank you
I used then to check if they enter anything, I will do something that is.
My teacher made me finish the program on the main program does not know how to declare from the subroutine .Thay help children PES A111
then in the main program you call it that is that.
dear teacher so the Produce section inp files with file read out is done individually or jointly teacher???
is made 1 Produce time or 2 times Produce 1 inp 1 out sir???
Particularly nhé.
to ask how to fix ASCII vertical bar in Pascal Za!?
I have not understand you?
Sensei,giá trị trả về là gì? Giá trị ko trả về là gì vậy thầy?
là sau khi thực hiện, nó đưa lại kết quả có thể lưu vào 1 biến ko. Bạn đọc lại bài một vài lần nhé.
Sensei, when should I use CTC and when should I not use it, teacher