Menu English Ukrainian russian Home

Free technical library for hobbyists and professionals Free technical library


Lecture notes, cheat sheets
Free library / Directory / Lecture notes, cheat sheets

Computer science and information technology. Procedures and functions (lecture notes)

Lecture notes, cheat sheets

Directory / Lecture notes, cheat sheets

Comments on the article Comments on the article

Table of contents (expand)

LECTURE № 3. Procedures and functions

1. The concept of an auxiliary algorithm

An algorithm for solving a problem is designed by decomposing the entire problem into separate subtasks. Typically, subtasks are implemented as subroutines.

A subroutine is some auxiliary algorithm that is repeatedly used in the main algorithm with different values ​​of some incoming quantities, called parameters.

A subroutine in programming languages ​​is a sequence of statements that are defined and written in only one place in the program, but they can be called for execution from one or more points in the program. Each subroutine is identified by a unique name.

There are two types of subroutines in Pascal, procedures and functions. A procedure and a function are a named sequence of declarations and statements. When using procedures or functions, the program must contain the text of the procedure or function and a call to the procedure or function. The parameters specified in the description are called formal, those specified in the call to the subroutine are called actual. All formal parameters can be divided into the following categories:

1) parameters-variables;

2) constant parameters;

3) parameter-values;

4) procedure parameters and function parameters, i.e. procedural type parameters;

5) untyped variable parameters.

The texts of procedures and functions are placed in the section of descriptions of procedures and functions.

Passing procedure and function names as parameters

In many problems, especially in computational mathematics, it is necessary to pass the names of procedures and functions as parameters. To do this, TURBO PASCAL introduced a new data type - procedural or functional, depending on what is described. (Procedural and function types are described in the type declaration section.)

A function and procedural type is defined as the heading of a procedure and a function with a list of formal parameters but no name. It is possible to define a function or procedural type without parameters, for example:

type

Proc = procedure;

After declaring a procedural or functional type, it can be used to describe formal parameters - the names of procedures and functions. In addition, it is necessary to write those real procedures or functions whose names will be passed as actual parameters.

2. Procedures in Pascal

Each procedure description contains a header followed by a program block. The general form of the procedure header is as follows:

Procedure <name> [(<list of formal parameters>)];

A procedure is activated with a procedure statement that contains the name of the procedure and the required parameters. The statements to be executed when the procedure is run are contained in the statement part of the procedure module. If a statement contained in a procedure uses a procedure identifier inside a procedure module, then the procedure will be executed recursively, that is, it will refer to itself when executed.

3. Functions in Pascal

A function declaration defines the part of the program in which the value is calculated and returned. The general form of the function header is as follows:

Function <name> [(<list of formal parameters>)]: <return type>;

The function is activated when it is called. When a function is called, the function identifier and any parameters necessary for its evaluation are specified. A function call can be included in expressions as an operand. When the expression is evaluated, the function is executed and the value of the operand becomes the value returned by the function.

The operator part of the function block specifies the statements that must be executed when the function is activated. A module must contain at least one assignment statement that assigns a value to a function identifier. The result of the function is the last value assigned. If there is no such assignment statement, or if it has not been executed, then the return value of the function is undefined.

If a function identifier is used when calling a function within a module, then the function is executed recursively.

4. Forward descriptions and connection of subroutines. Directive

A program may contain several subroutines, i.e. the structure of the program may be complicated. However, these subroutines can be at the same nesting level, so the subroutine declaration must come first, and then the call to it, unless a special forward declaration is used.

A procedure declaration that contains a forward directive instead of a statement block is called a forward declaration. At some point after this declaration, a procedure must be defined by means of a defining declaration. A defining declaration is one that uses the same procedure identifier but omits the list of formal parameters and includes a statement block. The forward declaration and the defining declaration must appear in the same part of the procedure and function declarations. Between them, other procedures and functions can be declared that can refer to the forward-declaration procedure. Thus, mutual recursion is possible.

The forward description and the defining description are the complete description of the procedure. The procedure is considered to be described using forward description.

If the program contains quite a lot of subroutines, then the program will cease to be visual, it will be difficult to navigate in it. To avoid this, some subroutines are stored as source files on disk, and if necessary, they are connected to the main program at the compilation stage using a compilation directive.

A directive is a special comment that can be placed anywhere in a program, where a normal comment can be. However, they differ in that the directive has a special notation: immediately after the closing bracket without a space, the sign S is written, and then, again without a space, the directive is indicated.

Example

1) {SE+} - emulate math coprocessor;

2) {SF+} - form a distant type of procedure and function call;

3) {SN+} - use math coprocessor;

4) {SR+} - check if the ranges are out of bounds.

Some compilation switches may contain a parameter, for example:

{$1 file name} - include the named file in the text of the compiled program.

Author: Tsvetkova A.V.

<< Back: Pascal language (Introduction to Pascal. Standard procedures and functions. Pascal operators)

>> Forward: Subroutines (Routine parameters. Subroutine parameter types. String type in Pascal. Procedures and functions for string type variables. Records. Sets)

We recommend interesting articles Section Lecture notes, cheat sheets:

Agricultural law. Lecture notes

National history. Lecture notes

Surgical diseases. Crib

See other articles Section Lecture notes, cheat sheets.

Read and write useful comments on this article.

<< Back

Latest news of science and technology, new electronics:

The existence of an entropy rule for quantum entanglement has been proven 09.05.2024

Quantum mechanics continues to amaze us with its mysterious phenomena and unexpected discoveries. Recently, Bartosz Regula from the RIKEN Center for Quantum Computing and Ludovico Lamy from the University of Amsterdam presented a new discovery that concerns quantum entanglement and its relation to entropy. Quantum entanglement plays an important role in modern quantum information science and technology. However, the complexity of its structure makes understanding and managing it challenging. Regulus and Lamy's discovery shows that quantum entanglement follows an entropy rule similar to that for classical systems. This discovery opens new perspectives in the field of quantum information science and technology, deepening our understanding of quantum entanglement and its connection to thermodynamics. The results of the study indicate the possibility of reversibility of entanglement transformations, which could greatly simplify their use in various quantum technologies. Opening a new rule ... >>

Mini air conditioner Sony Reon Pocket 5 09.05.2024

Summer is a time for relaxation and travel, but often the heat can turn this time into an unbearable torment. Meet a new product from Sony - the Reon Pocket 5 mini-air conditioner, which promises to make summer more comfortable for its users. Sony has introduced a unique device - the Reon Pocket 5 mini-conditioner, which provides body cooling on hot days. With it, users can enjoy coolness anytime, anywhere by simply wearing it around their neck. This mini air conditioner is equipped with automatic adjustment of operating modes, as well as temperature and humidity sensors. Thanks to innovative technologies, Reon Pocket 5 adjusts its operation depending on the user's activity and environmental conditions. Users can easily adjust the temperature using a dedicated mobile app connected via Bluetooth. Additionally, specially designed T-shirts and shorts are available for convenience, to which a mini air conditioner can be attached. The device can oh ... >>

Energy from space for Starship 08.05.2024

Producing solar energy in space is becoming more feasible with the advent of new technologies and the development of space programs. The head of the startup Virtus Solis shared his vision of using SpaceX's Starship to create orbital power plants capable of powering the Earth. Startup Virtus Solis has unveiled an ambitious project to create orbital power plants using SpaceX's Starship. This idea could significantly change the field of solar energy production, making it more accessible and cheaper. The core of the startup's plan is to reduce the cost of launching satellites into space using Starship. This technological breakthrough is expected to make solar energy production in space more competitive with traditional energy sources. Virtual Solis plans to build large photovoltaic panels in orbit, using Starship to deliver the necessary equipment. However, one of the key challenges ... >>

Random news from the Archive

Electric car acceleration record set 24.09.2023

Swiss electric racing car Mythen set an impressive world record, accelerating from zero to one hundred kilometers per hour in just 0,956 seconds. This result was recorded in the Guinness Book of Records and was recognized as the fastest acceleration among electric vehicles.

Mythen was created by students from ETH Zurich. This unique vehicle weighs just 140 kilograms and is equipped with an electric motor on each of its four wheels, providing a combined power of up to 240 kilowatts.

To achieve this level of acceleration, the students were able to improve traction by using a vacuum system with a fan. This system helps keep the Mythen closer to the road by removing air from underneath the car. This doubles the grip and therefore significantly increases acceleration.

The result achieved by Mythen surpassed last year's record of the student team from the University of Stuttgart, which took 1,461 seconds to accelerate to one hundred kilometers per hour.

Other interesting news:

▪ Microsoft servers will be equipped with their own processors

▪ GDDR6 memory chips

▪ New bipolar transistors MJL4281A (npn) and MJL4302

▪ Device for fighting pornography

▪ Ultraviolet for myopia

News feed of science and technology, new electronics

 

Interesting materials of the Free Technical Library:

▪ section of the site Cultivated and wild plants. Article selection

▪ article Specific analysis of a specific situation. Popular expression

▪ article What sound does the world's largest frog make? Detailed answer

▪ article Operator of hydraulic systems. Job description

▪ article Against telephone pirates. Encyclopedia of radio electronics and electrical engineering

▪ article Scheme, pinout (pinout) cable Panasonic G450, G500 + pinout. Encyclopedia of radio electronics and electrical engineering

Leave your comment on this article:

Name:


Email (optional):


A comment:





All languages ​​of this page

Home page | Library | Articles | Website map | Site Reviews

www.diagram.com.ua

www.diagram.com.ua
2000-2024