Lecture notes, cheat sheets
Computer science and information technology. Procedures and functions (lecture notes) Directory / Lecture notes, cheat sheets 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 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: ▪ Microsoft servers will be equipped with their own processors ▪ New bipolar transistors MJL4281A (npn) and MJL4302 ▪ Device for fighting pornography 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
Leave your comment on this article: All languages of this page Home page | Library | Articles | Website map | Site Reviews www.diagram.com.ua |