📄 pascal.txt
字号:
5.7) Writing a Function, and returns it's value:
**************************************************
Now, the only thing that the procedure is different from a function, is that the function can only
return one value (not like the procedure), and the way of returning that value is defferent than the
return of value in the procedure, as you remember, in the procedure we return values with 'Var',
right?! But in a function you do it with the function's name itself, I'll explain later.
Because now I'm going to exaplain you write the function first, and the then how to return it's
value.
5.7.1)Using function without any parameters passing:
function func_example: integer;
begin
writeln('it's all cool...');
func_example:=5;
writeln('man, I am good!!!');
end;
This is how you write a basic/simple function. But we almost never use those type of functions,
that don't receive any parameters into itself. Any way I'll explain you the example above:
do you see the ':' after the function's name, and after we see 'integer', this integer, is actualy
the type of value that this function may return, it could be any of the type of declaration, you
declare the variables them selfs.
Then as you can see, inside the function there is first a "writeln" command, wich will print
'it's all cool' on the screen, then you see that an undeclared variable receive into itself the value of 5, but if you'll look closely, you'll see, that the variable's name is the same as the function's, how you ask?! very simple, you see, when you make the function's name to receive some kind of
value, the function stops, well it isn't actualy stops, but you should stop writing the functin, to
save yourself useless mistakes. But there is another "writeln" command after receiving a
value by the function's name ( func_example:=5; ), and as I said before, and I'll say it again, you
should stop writing the function, after the retrieve of a value from the function, so the program will get to the "writeln('man, I am good!!!');" command, but you shouldn't do it, it's just wrong. :)
Now Iook at how you call, and receive a value from a function, and where does the value
goes to?!
program calling_a_function;
var
x, sum: integer;
{ here will come the function we wrote before }
begin { Main Begin }
x:=func_example; { the value of our function, that we wrote returns to x. }
writeln('One way to call a function, that returns: ',x);
writeln('Another way to call a function, that returns: ',func_example);
{ this is another way, which is more 'effective', when you print the returning value
the function, instead, of placing it into a variable, and then printing it on the screen! }
{ you only place the return value into a variable, when you know that you don't need it for
print out on the screen. please check out the threes way of use of the return value. }
sum:=func_example+func_example;
{ this line will call the function once, and add it's return value to the ruturn value of the
second function it will call, but you may do this act with variable and make actions on them
with the function's return value that way!!! }
end. { Main End }
5.7.2)Using function with parameters passing:
In paragraph 5.7.1, we learn how to write a basic/simple type of function (without any parameters
passing), now we'll see how the function will look like with parameters, and how to call this kind
of function. Check out the next example, of parameters passing in a function:
function func_example(num1, num2: integer): integer;
begin
func_example:=num1+num2;
end;
This function received two parameters, 'num1' and 'num2', then it added 'num1' to 'num2', and the result will go to 'func_example', that will be return from the function!
Now, look how to call a function with parameters passed into it:
program calling_a_function;
var
x, sum: integer;
begin { Main Begin }
x:=func_example(2,3); { the value of our function, that we wrote returns to x. }
writeln('One way to call a function, that returns: ',x);
writeln('Another way to call a function, that returns: ',func_example(2,3));
{ this is another way, which is more 'effective', when you print the returning value
the function, instead, of placing it into a variable, and then printing it on the screen! }
{ you only place the return value into a variable, when you know that you don't need it for
print out on the screen. please check out the threes way of use of the return value. }
sum:=func_example(2,3)+func_example(2,3);
{ this line will call the function once, and add it's return value to the ruturn value of the
second function it will call, but you may do this act with variable and make actions on them
with the function's return value that way!!! }
end. { Main End }
Simple isn't it?!
Final Note about the Functions/Procedures: all functions and procedures must be written before
the Main Begin!!!
But there is a small problem with the functions/procedures' calling, about that you'll learn in the
next chapter.
5.8) The problem with Procedures/Functions, and the solution:
**********************************************************************
You learn that you can can a function/procedure from another function/procedure, BUT you can't
call a function/procedure from another function/procedure, if the function/procedure you calling hasn't been written before the one it been called from! So, how can you call a function/procedure
from another function/procedure even if the function/procedure you tring to call was written after the function/procedure you calling from?!
It's truly simple, all you need to do is to declare the function/procedure the one you like to call
before all the inside of the functions/procedures are been written.
I'll show you two examples, the one which won't work, and the one that will:
program Won't_Work;
procedure calling;
begin
cool;
end;
procedure cool;
begin
writeln('You're a newbie!!! at list for now!!! :)');
end;
begin { main Begin }
calling;
end. { main End }
program Will_Work;
procedure cool; { declaring the 'cool' procedure, so we could call it from the 'calling' procedure,
even if the 'cool' procedure was written after 'calling' procedure! }
procedure calling;
begin
cool;
end;
procedure cool;
begin
writeln('You're a newbie!!! at list for now!!! :)');
end;
begin { main Begin }
calling;
end. { main End }
Now you may call the 'cool procedure from any where in the program!!! You see how simple his
is, so simple tha it scares me! :)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6) Uses...
6.1) What is a Uses file?!
***************************
A Uses file is kind of like a header file (if you know C/C++). A Uses file is a file which includes inside itself some functions, and when you'll include the Uses file, you may use it's functions.
You can use a ready Uses file of the compiler or to write your own functions and include them
inside a new Uses file (a *.tpu file, I'll get to it). Don't worry... I'll get to the part of how to use an
exist uses file, and how to write one.
6.2) Uses Crt:
**************
The most used Uses file in pascal programming is the Crt file, you can check for the file at the
pascal's Bin dir, the file called: "crt.tpu". Now... the most used functions of this Uses file are:
- clrscr
- textcolor
- textbackground
But first, check out the next example to see how to use a Uses file:
program Uses_Example;
uses crt; { the Uses file declaration Must come under the "Program", and only under it. }
var
...
In the above example we included the crt.tpu file in our program, so now you can use all the
Crt functions... Like:
- clrscr => this function will Clear/Clean the screan from all the text.
- textcolor => this function will change the color of the text, colors are from 1 to 15 (includes),
you can also use the name of the color, like: black, white, yellow, green, red, etc.
- textbackground => this function is the background of the text, not the background of the whole
screen... the colors are the same as in "textcolor".
The next example will show you how to use those functions:
program Crt_Example;
uses crt; { the declaration of the Crt file in the program }
begin { the Main begin. We skiped right to the "Begin", and didn't include the "Var" part,
because we don't need any variables here }
writeln('The normal text.'); { the good old gray color of the text }
clrscr; { here we clear the screen, and the above line will disappear }
textcolor(blue); { now instead of the original gray color of the compiler the new color will
be blue }
writeln('The colored text, in blue.); { this will show you a line on the screen in the blue color }
clrscr; { again we'll clear the screen, and the above line will disappear as well }
textbackground(yellow); { the new background of the text is now yellow }
writeln('The colored text, in blue on a yellow background of the text.'); { an example of how the
text will look on the yellow
background of the text }
clrscr; { here we clearing the text again, but his time instead of black background it'll be yellow }
{ because the last textbackground color was Yellow! }
writeln('The colored text, in blue on a yellow background of the whole screen.'); { shows you
how the text
will look like
on the 'total'
background }
end. { the Main end }
6.3) Writing your own Uses File:
***********************************
Well, let say we wrote some kind of function which we'll use in many programs...
For example let say you know that you need to include in all your programs the same 'about' function which will tell the user of the program, who the program was written by, etc.
In this small chapter I'll teach you how to make your own Uses file, just the Uses Crt file.
The struction of the Uses file is like that:
UNIT cool_fun; { the name of our Uses file is now 'cool_fun' }
{ so, just like the Crt file, you write this to under the "Program", to include
this new Uses file into your program: "uses cool_fun;" }
INTERFACE
{ here you may include another Uses file, like Crt, or any of yours }
{ you may also put here the declaration of "Type", if you planning to use Array passing
parameters }
{ you may as well include variables (global variables) which will be known inside this
Uses file, and I think in the main program as well. }
{ Now, here we will declare some functions/procedures or both! The functions/procedures
you'll decalre are the functions/procedures you will be able to call/use from the Main program,
in which, this Uses file is been declared. }
{ Note: There must be at list one function or procedure under the INTERFACE }
{ INTERFACE Summary: Every thing you will write in the INTERFACE could be used from the
main program, or from another Uses file, if this file was declared! }
IMPLEMENTATION
{ as like in the INTERFACE part you may declare variables, and call other Uses files,
the only differences between the INTERFACE and the IMPLEMENTATION, is that
what you declare in the IMPLEMENTATION, stays in IMPLEMENTATION, it's for the
Uses file use only! }
{ in this part you need to write the function/procedure itself, not only the declaration of it,
as we did in the INTERFACE part }
{ you may also include functions and procedures that haven't been declared in the
INTERFACE part, but you won't be able to use those functions/procedures, from any other
place than this Uses only }
{ you may call all functions/procedures, including the declared and the not declared ones from
any of the other functions/procedures, including the declared and the not declared ones }
BEGIN { this is the Uses file Begin }
{ This part will stay Empty, in 99% it does! So, don't write anything between the Begin and the
End. }
END. { Uses file End }
All this you will write and save in the type of *.PAS file, then when you'll compile that
*.PAS file, and the compiler will create a *.TPU file (Turbo Pascal Unit), this *.TPU file will be created
in the same dir as the *.PAS file was ( the *.PAS will stay, don't worry )
If you like to use this file, you need to place it in the same directory as the compiler's files
(inside the BIN dir).
This is it folks, this is how you write your own Uses file, easy, isn't it. And it's VERY useful in
Pascal Programming, just like the Header files in C/C++.
This was the last chapter of my truly cool tutorial, please read the Final Words!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
**) Final Words:
*****************
Well, that's the ending, the ending of our story, that the ending, the ENDING...
This is it! Men... you are free to go, to run wild on the fields of the cyber space,
Born to be wiiiiiiiild... I hope you understood the basics of Pascal. But the language isn't over here, hell no, you still got two topics to learn on your own, using Files and Graphics.
( Graphics and working with Files aren't that important in Pascal, but you're free to learn them! )
Personally I like to work/play with files, but in C! Because playing with files in C is
the most easy thing you had ever seen, so my advise to you is to learn C too.
If you think: "Why the heck do I need all this, if you haven't thought me Filing and Graphics?"
Then remember I promissed you a Black Jack game, here you go, just like I promised,
This will show you, that you don't need to know how to work with files or creating graphic and
stuff, you can create a really cool and color full game in a simple text mode!
This also a good Drill for you, fix the repeating of the cards and try to use less variables than me.
If you like more practice, ask the WebMaster of the site who published this tutorial to connect
me, and I will publish exercises, so you could practice at home by writing programs all by yourself!!!
And Remember this as well, "I didn't write this tutorial for myself, I wrote it for you"!
P.S: Please don't E-Mail with any questions, Sorry, but I'm too Busy...
So just go masturbate, as a wise man once said: "If you'll do it, it'll COME" ;)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
About the Source Code:
~~~~~~~~~~~~~~~~~~
I placed some notes inside the (* *) in the source code ( the (* *) and the { } are just the same ).
( But you can't open the note with (* and close it with }, or open with { and close with *), it's not
working that way. )
The places I put the notes in, are the places I think are the most important for you to understand
what's going on in the source code, and how the game works (the total idea).
Well, enjoy yourself... as for me, I going to bed, my girl is waiting for me :)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
The Source Code for the Black Jack game I was telling you about at the introduction section.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
program Devil_Black_Jack; { Devil_BJ
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -