Cainiao Q & A (function)

Source: Internet
Author: User

1. What is a function?
In C language, the program segment and subroutine used to complete a specific task are called functions. In a complete C program, a function is usually composed of many functions, and each function completes the specified task.

2. What is a function prototype?
A function includes the function name, function return value, and return value type, function parameters, and parameter types. All these features of a function are described as the prototype of the function. Function prototypes are generally placed at the beginning of the entire program file (internal functions ). For functions that can be used outside the current source file, it should be stated in a header file that the source file to use these functions must contain this header file.

3. What is the return value of a function?
After a function is executed, a value is passed to the called function, which is called the return value. Whether to return a value to the called function depends on the needs of the program. Sometimes, you do not need to return a value to the called function.

4. Why should we describe the function prototype?
The function prototype can tell the Compiling Program what parameters a function accepts and what return values will be returned. In this way, the Compilation Program can check whether the function is called correctly and whether there is an incorrect type conversion. Example:
Int some_func (INT, char *, long );
The compiler should check whether all calls to the function (including the definition of the function) use three parameters and return an int value. If the Compilation Program finds that the function call or definition does not match the function prototype, the Compilation Program should report an error or warning. For example, for the above function prototype, an error or warning message is reported when the compiler checks the following statements:
X = some_func (1);/* The number of parameters is small */
X = some_func ("Hello! ", 1," Dude! ");/* Parameter Type Error */
X = some_func (1, STR, 2879, "T");/* too many parameters */

The following function call is also incorrect, because the return value of function some_func () is not a long * type value.
Lvalue = some_func (1, STR, 2879);/* the return value of the function should be int rather than long. */
The compilation program can also check whether the function definition (or function body) matches the function prototype. For example, an error or warning message should be reported when the compiler checks the following function definitions:
Int some_func (char * string, long lvalue, int ivalue)/* The parameter location is incorrect */
In short, the source file indicates that the function prototype provides a mechanism to check whether the function is correctly referenced. Currently, many popular compilation programs will check whether the prototype of the referenced function has been described in the source file. If not, a warning will be issued.

5. How many parameters can a function have?
There is no definite limit on the number of parameters for a function, but too many parameters (for example, more than 8 parameters) are obviously an undesirable programming style. The number of parameters directly affects the speed of calling a function. The more parameters, the slower the function call. On the other hand, if the number of function parameters is small, the program looks concise and simple, which helps to check and find errors in the program. Therefore, we should try to reduce the number of parameters as much as possible, if a function has more than four parameters, consider whether the function is properly written. If a function has to use many parameters, you can define a structure to accommodate these parameters. This is a very good solution.
In the following example, the print_report () function requires 10 parameters. However, these parameters are not listed in its parameter description, but are obtained through an rpt_parms structure.
# Include <stdio. h>
Typedef struct
{
Int orientation;
Char rpt_name [25];
Char rpt_path [40];
Int destination;
Char output_file [25];
Int starting_page;
Char ending_page;
Char db_name [25];
Char db_path [40];
Int draft_quality;
} Rpt_parms;

Int print_report (rpt_parms *);
Void main (void)
{
Rpt_parms rpt_parm;
...
...
Rpt_parm.orientation = orient_landscape;
Rpt_parm.rpt_name = "qsales. rpt ";
Rpt_parm.rpt_path = "C:/reports ";
Rpt_parm.destingation = dest_file;
Rpt_parm.output_file = "qsales. txt ";
Rpt_parm.starting_page = 1;
Rpt_parm.ending_page = rpt_end;
Rpt_parm.db_name = "sales. DB ";
Rpt_parm.db_path = "C:/Data ";
Rpt_parm.draft_quality = true;

Ret_code = print_report (& rpt_parm );
....
....
}

Int print_report (rpt_parms * P)
{
Int RC;
....
....
Orient_printer (p-> orientation );
Set_printer_quality (p-> draft_quality = true )? Draft: normal );
....
....
Return RC;
}

The only disadvantage of the preceding example is that the compiling program cannot check whether the 10 members of the rpt_parms structure that reference the print_report () function meet the requirements.

6. What is an internal function?
Internal functions are described as static, and the scope is limited to the functions that indicate their source files. Scope refers to the visibility of functions or variables. If a function or variable is visible outside its source file, it is called a function or variable with a global or external scope. If a function or variable is visible in its source file, it is called local or internal scope.
Internal functions can only be used in the source file that describes them. If you want a function not to be used outside the source file, you should describe it as an internal function. This is a good programming habit, this avoids conflicts with functions of the same name in other source files.

Example:
# Include <stdio. h>
Int open_customer_table (void );
Static int open_customer_indexes (void );

Int open_customer_table (void)
{
Int ret_code;
....
....
If (ret_code = OK)
{
Ret_code = open_customer_indexes ();
}
Return ret_code;
}

Static int open_customer_indexes (void)
{
Int ret_code;
....
....
Return ret_code;
}

In the preceding example, the function open_customer_table () is an external function that can be called by any module. The function open_customer_indexes () is an internal function that will never be called by other modules. The reason for this is that the two functions are called by the open_customer_indexes () function only by open_customer_table.

7. If a function does not return a value, do you need to add a return statement?
In C language, the function described with the void keyword does not return values, and there is no need to add a return statement. In some cases, a function may cause a serious error. to exit the function immediately, add the return statement to skip the code that has not been executed in the function body. However, adding the Return Statement to the void function is a bad programming habit. However, the operations to exit the function in the Void function should be as concentrated and concise as possible.

8. How to pass the array as a parameter to the function?
When an array is passed as a parameter to a function, there are two methods: Value passing (pass by value) and address passing (transfer by reference. In the value transfer method, when describing and defining a function, add brackets to the end of the array parameter. when calling the function, you only need to set the address of the array (that is, the array name) passed to the array.
For example, in the following example, array X [] is passed to the byval_func () function by passing values.
# Include <stdio, h>

Void byval_func (INT []);/* the parameter of this function is an int array */

Void main (void)
{
Int X [10];
Int y;
For (y = 0; y <10; y ++)/* Initialize an integer array */
X [y] = y;
Byval_func (x);/* put the array name in the parameter, note that the value is passed */
}

Void byval_func (int I [])
{
Int y;
For (y = 1; y <11; y ++)
{
I [y] = y;
Printf ("% d"/N ", I [y]);
}
}

In the preceding example, an array named X is defined and an initial value is assigned to its 10 elements. The byval_func () function is described as follows:
Int byval_func (INT []);
The Int [] parameter tells the Compilation Program that byval_func () has only one parameter, that is, an array composed of an int type value. When the byval_func () function is called, you only need to pass the address of the array to this function, that is:
Byval_func (X );
In the value transfer mode, array X is copied, And the copied array is stored in the stack, which is then received and printed by the byval_func () function. Because the byval_func () function is passed as a test bay of the initial array, modifying the passed array within the byval_func () function has no effect on the initial array.
The overhead of the value transfer method is very large because of the following reasons: first, you need to completely copy the initial array and store the test bay in the stack, this will take a long time, so the efficiency of the value transfer method is relatively low. The test bay of the initial array needs to occupy additional memory space (memory in the stack, compile the program to generate a part of the code used to copy the initial array, which will increase the program.
The address transfer method overcomes the disadvantages of the value transfer method and is a better method. In the address transfer mode, the pointer to the initial array is passed to the function, and the initial array does not need to be copied. Therefore, the program is refined and efficient, and memory and stack space are also saved. In the address transfer mode, you only need to describe the function parameters in the function prototype as a pointer to the Data Type of the array element.
Example:

# Include <stdio. h>
Void const_func (const int *);
Void main (void)
{
Itn x [10];
Int y;
For (y = 0; y <10; y ++)
X [y] = y;
Const_func (X );
}

Void const_func (const int * I)
{
Int y;
For (y = 1; y <11; y ++)
{
I [y] = y;
Printf ("% d/N", * (I + y ));
}
}

In the preceding example, an array named X is also defined and the initial values are assigned to its 10 elements. The const_func () function is described as follows:
Int const_func (const int *);

The const int * parameter tells the compiling program that the const_func () function has only one parameter, that is, the pointer to an int type constant. When calling the const_func () function, you also need to pass the array address to the function, that is:
Const_func (X );

In the address transfer mode, the initial array is not copied and stored in the stack. The const_func () function only receives a pointer to an int type constant, therefore, when writing a program, ensure that the pointer passed to the const_func () function is an array consisting of int values. The const modifier prevents the const_func () function from accidentally modifying an element in the initial array.
The only disadvantage of address transfer mode is that the program itself must ensure that an array is passed to the function as a parameter. For example, in the prototype and definition of the function const_func, does not explicitly indicate that the parameter of this function is a pointer to an array consisting of int type values. However, the address transmission method is fast and efficient. Therefore, this method is recommended when the operation speed requirement is high.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.