?? pascal.txt
字號:
3) Arrays.
3.1) Array's definition:
***********************
First a few words about the Array, the array is a type of variable, the idea is that a simple
variable can't include inside itself more than one value... but the array type of variable CAN.
Under the "VAR" (in your program) you define the number of cells to the array, in the next
way:
program example;
var
arr: ARRAY [1..3] OF integer; { here we declared to the array variable called "arr", 3 cells, and
the value every cell can get is only an "Integer" type. }
begin { the main "Begin" }
...
The two dots betweet the 1 and the 3 means 'from where to where'. you can use as many cells
as you whish... You can use any name you like for the array, instead of "arr" in this small
example, you MUST keep this form:
arrayname: ARRAY [the_first_cell .. the_last_cell] OF the_type_of_value_in_every_cell.
Those cells makes the program more effective, and less work for you.
3.2) Using Arrays:
*******************
Ok... now you know how to difine an array, but you don't know how to use it, now look:
we said that the array is number of cells, right?! And we want to enter a specific cell and enter
a value into it, the next example will explain to you how to enter a value into a specific cell, and
how to read a value from a specific cell as well.
The next example will show you away to enter into the cell a value:
Begin {the main "Begin" }
arr[1]:=22; {the cell number 1 will include inside it the value 22 }
arr[2]:=5; {the cell number 2 will include inside it the value 5 }
arr[3]:=0; {the cell number 3 will include inside it the value 0 }
The idea is that the number of the Cell is in the [ ]. Remember that you can't use a number of
cell which is out of range (in this case the range is from 1(including 1) to 3(including 3), so you
CAN'T do the:
arr[4]:=1; { the number of the cell is 4, and our last cell number is 3, so the compiler will show
an 'Error'!!! REMEMBER that! }
Now, how do you read from the value from a specific cell?! Lets say i would like to print the the
cell number 2 on screen... look:
writeln(arr[2]); { This will print the value inside the cell 2 on the screen }
Just remember that "arr[2]" is like a simple variable, you can do what ever you like with it...
You can play any math on it, just like on a simple variable ( * , + , -), but it's 'div', or / depended
on the type of the value inside the cell (if it "Integer" then it's div, and if it's "Real" then it's / )
Ok... Lets move on...
3.5) Using Arrays and Loops:
********************************
Until now we wrote in the [ ] the number of the cell we want to use, but in 90% of working
with arrays, we never use a specific cell... We'll use Loops, the 'Index' of the loop instead of a
specific value (the specific number of the cell) in the [ ]... Check out the next example and you'll
get the idea:
program example_ARRAYS_LOOPS;
var
i: integer; { here we declared an "Integer" type variable, which we will use as an Index. }
begin { the main begin }
for i:=1 to 3 do { a simple "For" loop from 1 to 3 (including 1 and 3) }
arr[i]:=i+1; { every cell from 1 to 3 will get the INDEX, the "i"+1... so cell 1 will include 2,
cell 2 will include 3, and cell 3 will include 4 }
end. {the main end }
You can use any type of loop as you wish, and the number number of the cell is the Index, or you can use variables, as long as this variable will change every time the loop will repeat itself
( Like the variable is been Increased or Decreased, it's your choise! ) so you won't get into
a situation when you will use the same cell.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4) Strings.
4.1) What the heck Strings are?!
***********************************
Remember the "Char" type variables, well a String is just like an Array but every cell in this array
will be from "Char" type. You can use arrays of "Char" type, but this will be stupid... because
there is a special declaration of String, I'll explain how to declare and use the string in the next two sections.
4.2) String's definition:
************************
Here I'll compare the "Char" type array declaration, and the original String's type declaration.
1. Ok, lets start with the "Char" type array first:
program Char_Array;
var
fake_string: array[1..5] of char;
2. And now the original String's one:
program the_String;
var
str: string;
Using the original way is better because, it's much more simple and less to write, and you don't
always have to declare a specific range, but if you like to do it (if you know the number of chars
you're going to use, or you just like to save some memory space, or both. ), here is the way you
declare an exact number of chars in the string:
var
str: string[5];
And this will create a string variable of 5 chars.
4.2) Using basic String:
*************************
Ok, I'll explain you now the process of working with Strings, like compering two strings, making
an input to a string, pulling an output of the string, how to find out the length of the string, and making:
4.2.1)Comparing two strings, the direct way:
if (str1>str2) then
...
4.2.2)Comparing two strings, char by char:
for i:=1 to 10 do
if (str1[i]=str2[i]) then
...
4.2.3)Making a string input, the direct way:
readln(str1);
4.2.4)Making a string input, char by char:
for i:=1 to 10 do
readln(str1[i]);
4.2.5)Pulling an output of the string, the direct way:
writeln(str1);
4.2.6)Pulling an output of the string, char by char:
for i:=1 to 10 do
write(str1[i]);
4.2.7)Getting the length of the string, the only way:
x:=length(str1); { the variable x must be from an integer type ONLY. }
{ now 'x' includes the number of chars there are in the 'str1' string. }
Well, that's all you need to know on how you need to declare and use the string, simple hah!? :)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5) Procedures and Functions.
5.1) What the heck a Procedure is?!
****************************************
Until now we wrote our entire programs in one part!!! But this isn't effective, like lets take for
example a program which you need to print on the screen 3 different lines, at the beginning,
at the middle, and at the end. In each part you need print all 3 lines... so what... you will
write those lines at every part of the program again and again, it's stupid don't you think...?!
So, what we'll do is write a small "procedure" which will include inside itself those 3 lines...
But first I'll explain to you, what a procedure is. A procedure can include inside itself commands
and conditions checking, and every thing you learn until now. The procedure may be used from
any place at the Main of the programs (between the Main "Begin" and the Main "End."), the
procedure may be used from another procedure as well. If you haven't understood it so well, don't worry, after a few examples you'll get the idea. Let us continue...
5.2) Writing a Procedure:
***************************
The Procedures will be written before the Main "Begin".
To open a procedure you write: "Procedure" then the name of the procedure, and then ";",
simple, don't you think... Now, under it, you must put "Begin", (it's the law :) ) even if you have
only one command in the procedure. The procedure will end when you'll put "End;".
Look at the next example to get the idea:
program Procedure_example; { the program's title }
procedure line; { openning a procedure }
begin { the Procedure's "Begin" }
writeln(' Uncle Fucker, just like the song says.');
end;
begin { the Main "Begin" }
write('I am an'); { printing on the screen: "I am an", and staying on the same line... }
line; { using the procedure, it's like the code written inside the procedure is passed here... }
{ the 'writeln' command in the procedure will start from the last line, print " Uncle Fucker,
just like the song says.", and then, it will jump one line down. }
write('You are an');
line;
write('We all a bunch of ');
line;
end. { the Main "End" }
We didn't use "Var" in our small example program, because we didn't need any global variables in our example program.we could also declare some Local variables, by placing a "Var" before the procedure's "Begin", just like the regular "Var" we knew from the first chapters, except that:
Those Local variables will exist only in the procedure, and will be deleted if you won't store them
inside a Global variable (the variables we declared in the "Var" under the "Program" 's name,
as you know the global variable are known to the whole program, and the local variable are only
known to the procedure itself, to the specific procedure, not to all of the procedures.
5.3) Parameters' rules:
************************
Until now what we did, was only a simple procedure... This means that we could only use: Global
variable (the variable you declaring in the "Var" under the "Program" 's name) inside the procedure, but what we will do know is returning variable from the procedure, and inputting them
into it (the procedure)...
To do that you need to remember the next two rules:
1. When you send a variable to procedure, the type of variable you send to the procedure
is the same as the type of the variable which will receive the Data of the variable you sent.
I know it's like Chinese to you, or French to all the Chinese readers :)
2. If you like to send an Array to a procedure you need to write a "Type" definition, under the
"program" (before the "Var"). This is how you do it:
program Type_definition;
const
m = 5;
type
arr_type = array[1..m] of integer;
var
...
'arr_type' is now a new declaration type, this 'arr_type' could have any name as you like.
If you like to send an array to procedure, and you wrote the procedure so it will receive
an array from the 'arr_type' type, then the array which you'll send to this procedure
MUST have the same number of cells as the type the procedure receives. In the above
example of "Type" the array MUST be 5 cells long, from 1 to 5.
Now, after you understood the rules you may move on to the next section, which will explain
you: Who to use Parameters, how to send variables to another procedure and even to receive them back with a new Data in them... just continue reading!
5.4) Writing a Procedure with Parameters passing:
********************************************************
Well I hope you understood the rule, becuase we can't go on to the "writing a procedure with
parameters passing" part without those rules, so if you're not sure if you understood the rules,
please reread the rules chapter again and again until you'll under stand them perfectly!!!
But if you did understand them, let's go on...
The whole idea of parameters passing is to make the programming more simple and more
effective, like for example, take a look at the next procedure:
procedure example1;
begin
writeln('a newbie is a lamer :)');
end;
Now, instead of 'newbie' you could place any other word, isn't it?!
So, how about writing a procedure that will place a String type variable instead the word newbie,
so you may use this procedure in different places with a different meaning. You could do it like
this:
procedure example2(the_word: string);
begin
writeln('a ',the_word,' is a lamer :)');
end;
Now, you probably ask, "so... how do I use this procedure???". Very simple, look at this small
program:
program the_use;
var
name: string;
number: integer;
begin { the Main Begin }
name:='moron';
number:=25;
example2(name); { this line will print on the screen: "a moron is a lamer :)" }
example2('moron'); { this line will print the same thing as the line above, the variable 'name',
received inside itself the line: moron . }
example2(number); { this line won't work because, actually with this line in the code, you
won't be able even to compile the code, why you ask, then return back
and reread the rules chapter again, because the type of variable wich the
procedure receives does not much the type of variable you send to the
procedure!!! }
end. { the Main End }
Ok, now you can send more than one variable to a procedure, but you need to write a procedure
to receive that much variables, and when you send the values/variables to the procedure you
need to send them at the same order as the variables were writen in the procedure to receive
those values/variabels, they also must mutch the order they been written, it's all said in the
rules part!!!
In the next chapter you'll learn how to return values from the procedure as a parameter!
5.5) Returning Parameters in Procedures:
**********************************************
After you learn the parameters' rules, and you know how to write a procedure with them,
I may now teach you how to return values with them:
procedure return_example(var a: integer);
You see the littele "var" before the 'a' variable, this means that at the end of the procedure the
value which inside the variable 'a' will be returned to the variable you send to this procedure, and which the variable 'a' received it's value!
This way is mostly used, when you need to return more than one value, or you need to return special values, like Arrays, which is that the only way to do that!
In the next chapter you will see other ways to return one value only, without sending any other variable to pass the value.
5.6) What the heck is a Function?!
**************************************
Very good, you got this far, I'm proud of you, because now comes the GOOD part, but a little harder than the previous chapters, but don't worry >:)
Well, a function is actually a procedure, that must always return a value, even if the function
haven't received any values/variables into itself!
The only thing you need to know is that a Function can return one value only, in the section you will see how to write a function, how to return the value, and you will learn the rules of writing
a function, so, just continue reading!
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -