Now almost every major IT company is able to develop its own programming language, in which it will write its own solutions and products. In fact, many students in specialized universities are able to do the same. But old or, it is better to say, previous solutions in programming do not lose their relevance either.
Surely, the vast majority of school students learn a programming language such as Pascal (hereinafter - Pascal). Many consider it obsolete and useless, but in fact even now - in the age of Java, C ++ and other high-level programming languages - Pascal is relevant. So do not neglect the knowledge that is given on it (and for free), and it is clearly worth paying special attention to the procedures and functions in Pascal. They can help in the future if, for example, you decide to connect your life with high technology.
Communication with Delphi
It is worth noting how exactly the languages Delphi and Pascal are related. On the Internet you can often see articles in which the author refers specifically to Pascal when listing any properties or features of Delphi. Why it happens? The fact is that the language of Delphi itself is based precisely on Pascal. More precisely, there is a version of Object Pascal on it, which has been significantly redesigned and supplemented by some innovations exclusive to Delphi.
Program Examples
To understand why, you need to learn at least at the basic level the procedures and functions in Pascal. It is better to refer to examples of programs written in this language.
For example, the entire Total Commander - one of the most convenient file managers for Windows and Android, according to many users, is written specifically in Pascal.
Part of Skype for the Windows operating system, as well as part of the first version of Photoshop, were written just in the Pascal programming language.
Pascal-based offspring (Delphi) include: FL Studio, Guitar Pro, as well as games like Hero Battles, Space Empire 4, and Space Rangers.
Program structure
In order to correctly call and apply procedures and functions in Pascal, it is necessary to familiarize yourself with at least the approximate structure of a program in a given programming language. And to understand it is not difficult: everything is extremely logical.
There are two options for the layout of the program: brief and detailed. Information about each of them is presented below.
Brief
The program begins with the word program, after which the name of the program is written in English, and a semicolon is put at the end. Please note that the name must not coincide with the name of any variable described in one of the following sections.
Then you can designate constants (constant values with assigned symbols). To do this, enter “const”, then the constants themselves (for example, n = 5, etc.), you need to close the line with a semicolon.
The next step is to fill out the var section. It introduces variables of various types. For example, Integer or Real. The section is closed with a semicolon.
Then just follow the procedures and functions in Pascal. With a semicolon at the end.
The beginning of the program is defined by the word “begin”.
It is followed by the main body of the program.
The end is defined by the word "end.".
Detailed
In the detailed version, between the sections “program” and “const”, you can insert the string “label” to fill the label, and after “const” you can add “type” to describe data types.
True, if these lines are not required for the program to work, then they can be omitted. The program compiles successfully (if everything is done correctly).
Procedures and Functions
It is worth noting that all procedures and functions in Pascal also have their own structure. Moreover, it is similar to the structure of the program. Only instead of the word "program" in the beginning you need to write "procedure".
Each program has both local and global variables. Local are valid only for procedures and they act, respectively, only inside the body of the procedure. On the contrary, with global ones, they work throughout the program.
With functions, everything is similar. Only instead of the word "procedure" you need to write "function", and at the end also add the type of the return value.
in Pascal, the lines of procedures and functions are very convenient to apply for a number of reasons, which will be described later.
Pascal. Tasks of procedures and functions
Procedures and functions in the Pascal programming language exist at the subprogram level. That is, they can be filled in once, and then, when they will be needed the next time, just turn to their names, and not redial.
This greatly simplifies the task for the programmer when writing code, and also makes the code itself compact. This minimizes the chance of an error occurring in the program, because of which the compiler may fail to execute it or conduct it incorrectly.
It is the procedures and functions that allow the programmer to pass parameters by reference. By the way, this type of information transfer inside the program code is used in a huge number of programming languages (if not all). But only if in other cases this is done only through functions, then in Pascal it is also done through procedures.
Examples
Now we will analyze examples of functions and procedures in Pascal: how to find the sum of two numbers (functions) using a procedure and a function. It should be noted that both of these structures are inserted between the "var" and "begin" sections.
Procedure:
Procedure summa (a, b: integer; var c: integer);
Begin
c: = a + b;
end;
Function:
Function summa (a, b: integer): integer;
Begin
summa: = a + b;
end;
Standard procedures and functions in Pascal
Standard procedures and functions include actions such as addition and subtraction, multiplication and division.
Just need to consider the following feature:
Integer has a limitation: it only works with integers. Its alternative is the type Real. With its help it is possible to carry out operations of division and multiplication.
Standard procedures and functions also include processes for comparing numbers with each other or with other numbers. With this, you can find the minimum and maximum values in the array. Yes, in Pascal, as in most other programming languages, you can work with arrays.
It is very convenient to work with arrays using procedures and functions. To fill it, you need to start a cycle, run it and complete. Well, if you only need to form it. If you need to perform any actions inside the array, then its entire construction will stretch to several lines.
What to do if the same action with an array needs to be performed several times? Is it really convenient to rewrite it every time again? Of course not. In this case, the function or procedure will come to the rescue (it all depends on the specific situation).