C Language Improvement--function application (parameter, recursion)

Source: Internet
Author: User

Understanding:

To use a good function should have a full understanding of the function, if not fully understand the function, it will be easy to use to create problems. You can refer to this blog for the understanding of functions: The function is understood again!. Below is the bottom of the application to do some of the relevant instructions.

Parameters:parameter passing:

First of all, the transfer of function parameters in C language is called in the way of the value of the transfer. To implement a function that appears to be addressed in other languages, the number of addresses to which the number of operations is to be manipulated is passed as a parameter. In fact, all the transmission is a number of transmission, but the address of the transmission is the data. Just as all the arrays are actually one-dimensional, just for the convenience of people who are artificially divided into two and multidimensional.

variable parameter list:

The functions we use generally are parameters and types, but for example, the number and type of arguments that are passed at times require the user to obtain them during the run of the software, then a mutable parameter list is required.

The mutable parameter list is implemented by STDARG this macro. This macro is defined in the Stdarg.h header file and defines a va_list type and three macros for Va_start, Va_arg, and Va_end. function declarations in a defined form can be as follows:

<span style= "FONT-SIZE:24PX;" > function name (int n_values,... ) </span>

Where 1, Va_start is used for initialization, the parameter is the va_list variable name and the last known argument before the ellipsis. 2. Va_arg is used to access and return parameters, the parameter is the va_list variable name and the next parameter type of the parameter list. 3. Va_end is used to return from the function after the last fetch of all parameters, the parameter is the va_list variable name.

use it--recursion:

When it comes to recursion, people are already familiar with the simple definition of a function that calls itself directly or indirectly. But we often show examples of recursion: the example of calculating factorial and Fibonacci numbers does not fundamentally explain the true purpose of recursive functionality.

Here's what we often use to illustrate recursive routines: use recursive implementations to calculate the factorial of N.

<span style= "FONT-SIZE:24PX;" >/*** computes the factorial */longfactorial (int n) {if (n <= 0) return 1;elsereturn n * factorial (n-1) of n with a recursive method;} </span>

However, recursion is flawed, because there is a lot of runtime overhead associated with each recursive invocation of a function, but if the function does not perform any tasks after the recursive call is returned, the recursive function can be replaced with an iterative calculation, although it is necessary to make a tradeoff between readability and operational overhead. Below is the process that the top program implements with an iterative approach.

<span style= "FONT-SIZE:24PX;" >longfactorial (int n) {int result = 1;while (n > 1) {result *= n;n-= 1;} return result;} </span>

In fact, for this example, the readability of the two is not too much, but the latter is much less expensive to run than the former. It seems that the latter is better than the former, but when should we use recursion? Look at this program below: Accept an integer value (unsigned) and convert it to character printing.

<span style= "FONT-SIZE:24PX;" >/*** accepts an integer value (unsigned), converts to characters, and prints. */#include <stdio.h>voidbinary_to_ascii (unsigned int value) {unsigned int quotient;quotient = value/10;if (quotie NT! = 0) binary_to_ascii (quotient);p Utchar (value% 10 + ' 0 ');} </span>

This program and the first program comparison can be found that the function in the program after the call itself has done some other operations, and the first program did nothing. In contrast, it can be found that the more powerful recursive function is, in the function when the function calls itself, that is, after each layer of the function is called back, it is more appropriate to use recursion when the return information to do further work. Understanding this in depth requires a fundamental understanding of recursion.

C Language Improvement--function application (parameter, 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.