MENU service case
 Website construction website design Beijing website construction high-end website production company Shangpin China
We create by embracing change
360 ° brand value__
simplified Chinese character
Simplified Chinese English

Shangpin China Joins Hands with Beisheng Internet to Create a New Chapter in Website Construction

Type: Shangpin Dynamic Learn more

Function application in program

Source: Shangpin China | Type: website encyclopedia | Time: 2015-06-17
In programming language Beijing website production In the development process, process abstraction is an important and milestone concept. Process abstraction means that a statement sequence can be written into a subroutine with name and parameters in the program, and then called where needed. It realizes the abstraction of computing process and the reuse of code.

When people solve a complex problem, they usually adopt the method of gradual decomposition and divide and conquer, that is, a complex big problem is decomposed into several small problems that are easier to solve, and then solved separately. Similarly, when designing a complex program, programmers often divide the whole program into several relatively independent subprograms with single functions, and then implement them separately, and finally assemble all subprograms like building blocks.

Procedure and function are two different kinds of subprograms. A procedure is a sequence of statements calculated parametrically. Generally, there is no return value. Functions are similar to procedures in structure, but a function usually has a return value. In C, C++and other languages, procedures and functions are unified as functions.

When using functions in programs, you need to define and call functions. Each programming language provides rules for function definition and calling. Function definitions describe what and how a function does. Function calls use defined functions.

1. Function definition

The definition of a function usually consists of two parts: the function head and the function body. The first part of the function gives a contract that explains how to use a function; The P number body describes the specific functions and implementation details of the function with statements. The general format of function definition is:

Return value type function name (formal parameter list),/function header
{
Function body;
}
The data header describes the data type of the return value of the function, the name of the function, and the parameters (called formal parameters) and their types required when the function runs. The function implemented by the function is described in the function body.

2. Function call

When a function (called function) needs to use a function already implemented by another function (called function), it is called by name and parameters, called function call. When using a function, you only need to know how to call it, and you don't need to care about the internal implementation of the called function. Therefore, the calling function needs to give the name of the called function, the parameters (actual parameters) passed to the called function, and how to handle the return value.

The general form of function call is:

Function name (actual parameter table);

When the function is called, the system needs to do a series of processing, including saving the site, transferring actual parameters, controlling the transfer to the called function, etc. After the call is completed, it needs to restore the site, transfer the return value, and control the transfer back to the called function.

Table 7-4 is an example of function definition and function call, where the function of function power is to solve the k power of real number x.
 Function definition and function call

 


The meaning of function call is shown in Figure 7-7, where the calling function caller calls the function power in its statement "y=power (a, 4);". Therefore, during the execution of the caller, when the function is called, the values of the actual parameters a and 4 are passed to the formal parameters x and k to control the flow function power to start execution. When the return p is encountered, the return value is calculated, and power execution ends. After that, The control flow returns the calling function caller and passes the return value to y, and then executes the statement after "Y=power (a, 4);" in the caller.
 Function Call Example


3. Parameters and parameter transmission

When a function is used in a program, parameters play the role of information transfer between the calling function and the called function. There are usually two types of parameters: formal parameters and actual parameters. The formal parameter (formal parameter for short) appears at the beginning of the function when the function is defined, in the form of characteristic parameter representing some quantity; The actual parameters (Arguments for short) appear in the function call, and they correspond to the specific contents of the formal parameters.

For example, in the previous definition of the function power, the first part of the function is "double power (double x, int k)", where x and k are formal parameters. The function calls power (3 5, 4) Realize calculation 3 54, 3.5 and 4 are actual parameters. The function calls power (a, m) to calculate am. A and m are actual parameters. The value of the actual parameter variable a needs to be passed to parameter x, and the value of the actual parameter variable m needs to be passed to parameter k. The. actual parameters a and m must have certain values when called.

The actual parameters of the function correspond to the formal parameters. When implementing a function call, the actual parameter must pass a value to the formal parameter. Parameter passing refers to the matching action of actual participation in formal parameters during function call. If a value is passed from an actual parameter to a formal parameter, the corresponding call is called an optimization call; If the address of the actual parameter is passed (or passed by reference), it is called a reference call. Taking the function that realizes the exchange of two integer variable It values as an example, the following briefly describes the problem of parameter transfer in the value transfer call and reference call methods. Swapl uses value transfer calls, and swap2 uses reference calls. Their definitions and calls are shown in Table 7-5.
 Parameter transfer refers to the matching action of actual participation in formal parameters during function call

In the value passing calling method, the formal parameter will obtain the value of the actual parameter. In this way, the formal parameters and actual parameters correspond to different storage units respectively. Taking the function swapl as an example, before the function call statement swapl (a, b) is executed, the system has not allocated storage units for the formal parameters, as shown in Figure 7-8 (a). When the function call statement is executed, the system allocates storage units for the formal parameters x and y, and the values of the actual parameters a and b are passed to the formal parameters x and y respectively. When the function swapl executes, the values of x and y are indeed exchanged, but at this time, x is independent of a and y is independent of b, so the values of a and b are not exchanged, as shown in Figure 7-8 (c). In fact, after the execution of the called function is completed, the storage space of the formal parameter is reclaimed by the system.

 Actual parameters and formal parameters correspond to different storage spaces


In the reference invocation mode, the formal parameter represents the storage location of the actual parameter, and the formal parameter can be regarded as a synonym of the storage location of the actual parameter. Therefore, in the function swap2, exchanging x and Y values is equivalent to exchanging a and b values. After the call, the exchange of a and b values is realized.

4. Standard Library Functions

When writing a program, it is not necessary and should not start from scratch, but should make use of the achievements of predecessors as much as possible to improve the speed and quality of development. For this reason, each programming language includes some function libraries provided with the program development environment when it is released for programmers to call when developing programs, such as the standard library of C language. The function library provided by compiler developers and third parties realizes many common or commonly used functions, and these functions are more reliable in accuracy, efficiency and portability.

The functions stored in the function library are called library functions. Library functions have clear functions, population parameters and return values.

The function library is provided as a file. For example, files with the extension lib or dll are usually some kind of function library.
Source Statement: This article is original or edited by Shangpin China's editors. If it needs to be reproduced, please indicate that it is from Shangpin China. The above contents (including pictures and words) are from the Internet. If there is any infringement, please contact us in time (010-60259772).
TAG label:

What if your website can increase the number of conversions and improve customer satisfaction?

Make an appointment with a professional consultant to communicate!

* Shangpin professional consultant will contact you as soon as possible

Disclaimer

Thank you very much for visiting our website. Please read all the terms of this statement carefully before you use this website.

1. Part of the content of this site comes from the network, and the copyright of some articles and pictures involved belongs to the original author. The reprint of this site is for everyone to learn and exchange, and should not be used for any commercial activities.

2. This website does not assume any form of loss or injury caused by users to themselves and others due to the use of these resources.

3. For issues not covered in this statement, please refer to relevant national laws and regulations. In case of conflict between this statement and national laws and regulations, the national laws and regulations shall prevail.

4. If it infringes your legitimate rights and interests, please contact us in time, and we will delete the relevant content at the first time!

Contact: 010-60259772
E-mail: [email protected]

Communicate with professional consultants now!

  • National Service Hotline

    400-700-4979

  • Beijing Service Hotline

    010-60259772

Please be assured to fill in the information protection
Online consultation

Disclaimer

Thank you very much for visiting our website. Please read all the terms of this statement carefully before you use this website.

1. Part of the content of this site comes from the network, and the copyright of some articles and pictures involved belongs to the original author. The reprint of this site is for everyone to learn and exchange, and should not be used for any commercial activities.

2. This website does not assume any form of loss or injury caused by users to themselves and others due to the use of these resources.

3. For issues not covered in this statement, please refer to relevant national laws and regulations. In case of conflict between this statement and national laws and regulations, the national laws and regulations shall prevail.

4. If it infringes your legitimate rights and interests, please contact us in time, and we will delete the relevant content at the first time!

Contact: 010-60259772
E-mail: [email protected]