C language - nested call of function, chain access, function declaration and definition, function recursion.

Nested calls of functions, chained access.

Functions can be called nestedly, but they cannot be defined nestedly, that is, functions cannot be defined again inside a function. But inside a function you can call other functions.

Nested call: It is to call another function inside a function.

Chain access: use the return value of one function as the parameter of another function.

The following figure is chain access, using the return value of strlen as the parameter of printf.

//The return value of printf is the number of characters printed (note that adding \n after %d, spaces, etc. are all characters), if an error occurs, it returns a negative number

Function declaration and definition

Function declaration:

1. Tell the compiler what a function is called, what its parameters are, and what its return type is. But whether it exists or not, the function declaration cannot decide.

2. The declaration of the function generally appears before the use of the function. It must be declared before use.

3. The declaration of the function should generally be placed in the header file.

For example

If the definition of the function is after the call, a warning will pop up saying that the function Add is undefined, such as Figure 2. At this time, you can declare it before the function call, such as Figure 2. The name of the formal parameter can be omitted or omitted. That is, a and b in Figure 2 can be omitted and written as int Add(int,int);

 Definition of function: The definition of function refers to the specific realization of the function, explaining the function realization of the function.

 Note: The declaration of the function is generally not used, and it is rarely used in practice when writing code. If there is only one function, can't it be defined directly in front? No need to declare. In an enterprise, an addition module is generally created --- creating an Add.c and Add.h and combining these two is called an addition module.

 Put the definition of the function in Add.c (source file 2), and put the declaration of the function in Add.h (header file). When using it, directly refer to the header file #include "Add.h" to use it in the original source file.  

 If you don't want to expose the function definition code, you can compile Add.c into a static library. Just open another project and create two files of Add.h and Add.c. Then, click the project name, right-click properties, general-configuration type-static library.lib-generate solution. Then an Add.lib file will be generated under the Debug of that project file. Opening the Add.lib file is binary information that cannot be read. At this time, the Add.h and Add.lib files can be used by others, and others can use this function by adding these two files to the project file. It can be realized at this time, the declaration is exposed to you but the implementation of the function is not shown to you. It is very similar to library functions. 

 Function recursion (recursion and regression)

A programming technique in which a program calls itself is called recursion.

Recursion as an algorithm is widely used in programming languages. A process or function has a method of calling itself directly or indirectly in its definition or description. It usually transforms a large complex problem layer by layer into a smaller problem similar to the original function to solve . Recursive strategy , only need a small number of programs to describe the multiple repeated calculations required in the problem-solving process, which greatly reduces the amount of code in the program.

The main way of thinking is: make big things small.

Each recursion is a complete independent function call.

Take the simplest recursion:

 Although this is an error (stack overflow), this is indeed a recursion of a function.

Example: Accept an integer value and print each bit of it in order. Input 1234, output 1 2 3 4 This is the recursion of the print() function. n>9 is the condition for recursive jumping out, otherwise there will be wireless recursion and it will not be possible to return

Two necessary conditions for recursion (if there is no recursive code, it must be wrong)

There are constraints, and when this constraint is met, the recursion will no longer continue.

It gets closer and closer to this limit after each recursive call.

Example:

1. Write a function that allows the creation of temporary variables to find the length of a string.

 2. It is not allowed to create a temporary variable when writing a function to find the length of the string.

 


Recursion and iteration

Recursion can sometimes be written as iteration, and iteration can sometimes be written as recursion.

1. Find the factorial of n.

 2. Find the nth Fibonacci number. (overflow is not considered)

Fibonacci sequence: The sum of the first two numbers equals the third number. fib(n) = fib(n-1) + fib(n-2)

 It is obvious that the second type of non-recursive is more efficient. Fewer operations and faster operations.

When writing recursion, there is no obvious defect. We use recursion to complete it at this time, otherwise it must be completed with non-recursion.

 hint:

Many problems are explained in a recursive form, simply because it is clearer than a non-recursive form

However, the iterative implementation of these problems is often more efficient than the recursive implementation, although the code readability becomes worse

When a complex problem cannot be implemented by iteration, the simplicity of recursive implementation can compensate for the running overhead it brings.

Classic example of function recursion:

1. Tower of Hanoi problem.

2. The frog jumps the stairs. Suppose there are n steps and a frog jumps one or two steps at a time, how many possibilities are there in total.

Guess you like

Origin blog.csdn.net/zxf123567/article/details/123435031