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. Subroutines (lecture notes)

Lecture notes, cheat sheets

Directory / Lecture notes, cheat sheets

Comments on the article Comments on the article

Table of contents (expand)

LECTURE No. 4. Subroutines

1. Subprogram parameters

The description of a procedure or function specifies a list of formal parameters. Each parameter declared in a formal parameter list is local to the described procedure or function, and can be referenced in the module associated with that procedure or function by its identifier.

There are three types of parameters: value, variable, and untyped variable. They are characterized as follows.

1. A group of parameters without a preceding keyword is a list of value parameters.

2. A group of parameters preceded by the const keyword and followed by a type is a list of constant parameters.

3. A group of parameters preceded by the var keyword and followed by a type is a list of untyped variable parameters.

4. A group of parameters preceded by the var or const keyword and not followed by a type is a list of untyped variable parameters.

2. Types of subroutine parameters

Value parameters

A formal value parameter is treated as a variable local to the procedure or function, except that it derives its initial value from the corresponding actual parameter when the procedure or function is invoked. Changes that a formal value parameter undergoes do not affect the value of the actual parameter. The corresponding actual value of the value parameter must be an expression, and its value must not be a file type or any structure type containing a file type.

The actual parameter must be of a type that is assignment compatible with the type of the formal value parameter. If the parameter is of type string, then the formal parameter will have a size attribute of 255.

Constant Parameters

Formal constant parameters work similarly to a read-only local variable that gets its value when a procedure or function is invoked from the corresponding actual parameter. Assignments to a formal constant parameter are not allowed. A formal constant parameter also cannot be passed as an actual parameter to another procedure or function. A constant parameter corresponding to an actual parameter in a procedure or function statement must follow the same rules as the actual parameter value.

In cases where a formal parameter does not change its value during the execution of a procedure or function, a constant parameter should be used instead of a value parameter. Constant parameters allow the implementation of a procedure or function to protect against accidental assignments to a formal parameter. In addition, for struct and string type parameters, the compiler can generate more efficient code when used instead of value-parameters for constant-parameters.

Variable parameters

A variable parameter is used when a value must be passed from a procedure or function to the calling program. The corresponding actual parameter in a procedure or function call statement must be a variable reference. When a procedure or function is invoked, the formal parameter-variable is replaced by the actual variable, any changes in the value of the formal parameter-variable are reflected in the actual parameter.

Within a procedure or function, any reference to a formal variable parameter results in access to the actual parameter itself. The type of the actual parameter must match the type of the formal variable parameter, but this restriction can be circumvented by using an untyped variable parameter).

Untyped Parameters

When the formal parameter is an untyped variable parameter, then the corresponding actual parameter can be any reference to a variable or constant, regardless of its type. An untyped parameter declared with the var keyword can be modified, while an untyped parameter declared with the const keyword is read-only.

In a procedure or function, an untyped variable parameter has no type, i.e., it is incompatible with variables of all types until it is given a specific type by variable type assignment.

Although untyped parameters provide more flexibility, there are some risks associated with using them. The compiler cannot check the validity of operations on untyped variables.

Procedural Variables

After defining a procedural type, it becomes possible to describe variables of this type. Such variables are called procedural variables. Like an integer variable that can be assigned a value of an integer type, a procedural variable can be assigned a value of a procedural type. Such a value could, of course, be another procedure variable, but it could also be a procedure or function identifier. In this context, the declaration of a procedure or function can be viewed as a description of a special kind of constant whose value is the procedure or function.

As with any other assignment, the values ​​of the variable on the left side and on the right side must be assignment compatible. Procedural types, to be assignment compatible, must have the same number of parameters, and the parameters in the corresponding positions must be of the same type. Parameter names in a procedural type declaration have no effect.

In addition, to ensure assignment compatibility, a procedure or function, if it is to be assigned to a procedure variable, must satisfy the following requirements:

1) it should not be a standard procedure or function;

2) such a procedure or function cannot be nested;

3) such a procedure must not be an inline procedure;

4) it must not be an interrupt procedure.

Standard procedures and functions are the procedures and functions described in the System module, such as Writeln, Readln, Chr, Ord. Nested procedures and functions with procedural variables cannot be used. A procedure or function is considered nested when it is declared within another procedure or function.

The use of procedural types is not limited to just procedural variables. Like any other type, a procedural type can participate in the declaration of a structural type.

When a procedure variable is assigned the value of a procedure, what happens at the physical layer is that the address of the procedure is stored in the variable. In fact, a procedure variable is very similar to a pointer variable, only instead of referring to data, it points to a procedure or function. Like a pointer, a procedural variable occupies 4 bytes (two words) that contains a memory address. The first word stores the offset, the second word stores the segment.

Procedural Type Parameters

Since procedural types can be used in any context, it is possible to describe procedures or functions that take procedures and functions as parameters. Procedural type parameters are especially useful when you need to perform some common action on multiple procedures or functions.

If a procedure or function is to be passed as a parameter, it must follow the same type compatibility rules as the assignment. That is, such procedures or functions must be compiled with the far directive, they cannot be built-in functions, they cannot be nested, and they cannot be described with the inline or interrupt attributes.

Author: Tsvetkova A.V.

<< Back: Procedures and functions (The concept of an auxiliary algorithm. Procedures in Pascal. Functions in Pascal. Anticipatory descriptions and connection of subroutines. Directive)

>> Forward: Files (Files. File operations. Modules. Types of modules)

We recommend interesting articles Section Lecture notes, cheat sheets:

Culturology. Crib

Pedagogy. Crib

Finance and credit. 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 bus on hydrogen 06.12.2001

The use of hydrogen as a fuel for vehicles is not new. But the bus, designed by the international company Daimler-Chrysler, is different in that the combustible gas does not explode in the cylinders of its engine, but quietly oxidizes in the fuel battery, giving electricity, and it already rotates the electric motors hidden in the wheel hubs. The supply of hydrogen (cylinders are installed on the roof) is enough for 300 kilometers at a speed of up to 80 kilometers per hour.

The novelty is now being tested in Hamburg, Stuttgart, London, Amsterdam, Reykjavik, Stockholm and several other European cities.

Other interesting news:

▪ Transparent and stretchable lithium-ion battery

▪ MAX17558 Dual Channel 60V DC-DC Buck Controller

▪ Human health depends on trees

▪ Beak repair using a computer

▪ Wet windows

News feed of science and technology, new electronics

 

Interesting materials of the Free Technical Library:

▪ section of the Electrician website. PUE. Article selection

▪ article Ten commandments operator. video art

▪ article How widespread was torture by the iron maiden in the Middle Ages? Detailed answer

▪ article Chemical analysis laboratory assistant at oil products supply enterprises. Standard instruction on labor protection

▪ article Ringer for concentrators. Encyclopedia of radio electronics and electrical engineering

▪ article Transceiver prefix to the R-250 receiver. 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