C and pointer (pointers on C) -- Chapter 7: Functions (I)

Source: Internet
Author: User
Chapter 7 Functions
This chapter is not very abusive to those who have a certain C base and have a certain excellent code style. The stdarg macro may be unfamiliar, and is responsible for defining the variable parameter list.


Summary:
Do not mention the new and old style. Eight hundred years ago.
A common function is to place the prototype in a separate file. When other files require the prototype, use the # include command to include the file, this technique can minimize the number of copies required for the prototype and help improve the maintainability of the program.
The return statement is used to specify the value returned from a function. If no return value is returned, it is void.
Function parameters are converted by passing values. A copy of the actually passed real parameters is required. Therefore, a function can modify the copy of its form parameter without changing the original value of the parameter.
The array name is also passed by passing the value, but it is passed to the function as a copy of the pointer. When an array parameter uses a subscript reference operation, indirect access operations are triggered. The actual access is an array element.
The abstract data type (ADT) is also called the Black Box. It consists of two parts: interface and implementation.
Interfaces are common, indicating how the customer uses the functions used by the ADT.
The implementation is private and actually executed.
Recursive functions call themselves directly or indirectly.
Some Recursion, such as tail recursion, can be implemented through iteration, which is much more efficient. Especially the Fibonacci series.
Variable Parameter quantities and types. They can be implemented using the macro defined in the stdarg. h header file. Variable parameters can be accessed from the first to the last.
The following example shows the variable parameters.


Warning:
1. incorrectly compile the function prototype in the scope of other functions.
Well, pay attention to the link property of the function. This function cannot be used in code blocks of other functions.
2. No prototype is written for functions whose return values are not integers.
Because the default value of the returned value is an integer.
3. Use the wrong parameter type in va_arg, resulting in undefined results.
Let's look at an example:
# Include <stdarg. h>
/* Take the average value of the parameter */
Float average (INT n_value ,...)
{
Va_list var_arg;
Int count;
Float sum = 0;

/** Prepare variable access parameters */
Va_start (var_arg, n_values );
/* Add a value from the variable parameter list */
For (COUNT = 0; count <n_values; count + = 1)
{
Sum + = va_arg (var_arg, INT );
}
/* Complete processing of variable parameters */
Va_end (var_arg );
Return sum/n_values;
}
The parameter type must be explicitly stated in va_arg (var_arg!


Programming tips:
1. Use the parameter name in the function prototype. You can provide more information to function users.
Use the parameter name whenever possible:
Int func (INT, INT );
From the function name to the form parameter Shi.
2. abstract data types can reduce the implementation details of modules and improve reliability.
3. When recursion is clearly defined, it can compensate for its efficiency. use recursion.


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.