Lecture notes, cheat sheets
Computer science and information technology. Subroutines (lecture notes) Directory / Lecture notes, cheat sheets 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: See other articles Section Lecture notes, cheat sheets. Read and write useful comments on this article. Latest news of science and technology, new electronics: The existence of an entropy rule for quantum entanglement has been proven
09.05.2024 Mini air conditioner Sony Reon Pocket 5
09.05.2024 Energy from space for Starship
08.05.2024
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 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 Ringer for concentrators. Encyclopedia of radio electronics and electrical engineering
Leave your comment on this article: All languages of this page Home page | Library | Articles | Website map | Site Reviews www.diagram.com.ua |