4.18 Notes (function)

What is a function?

    The definition of function in Wikipedia: subroutine.

    · In computer science, a subroutine is a certain part of the code in a large program, composed of one or more statement blocks. It is responsible for completing a specific task and is relatively independent.
    ·In general, there will be input parameters and return values, providing encapsulation of the process and hiding of details. These codes are usually integrated into software libraries.

Classification of functions in c language:

  1. Library Functions
  2. Custom function

Library function : generally refers to the function provided by the compiler that can be called in the c source program.

Why are there library functions?
The use of library functions is to improve efficiency. Such as our frequent use (printf).

Learn about library functions can refer to this website: http://www.cplusplus.com/ .

Use library functions must include the #includecorresponding header files.

Custom function: a function designed by the programmer himself. Like library functions, it also includes the function name, return value type and function parameters.

c language function which can not return a value, default return value of inttype. If there is really no return value, you can write it void, or there can be no formal parameters in the function, and it can be written void.

Such a function:

Max(int m, int n)
{
    
    
	return m > n ? m : n;
}
int main()
{
    
    
	int x = 5;
	int y = 4;
	printf("%d\n", Max(y, x));
	return 0;
}

The result is: the
Insert picture description here
default is an int type return, generally in a custom function, it is recommended to add a return type.

The parameters of the function:

  1. Actual parameters (actual parameters): The parameters actually passed to the function are called actual parameters. The actual parameters can be: constants, variables, expressions, functions, etc. No matter what kind of quantity the actual parameters are, they must have certain values ​​when the function is called in order to pass these values ​​to the formal parameters.
  2. Formal parameters (formal parameters): Formal parameters refer to the variables in parentheses after the function name. Because formal parameters are instantiated (allocated memory units) only in the process in which the function is called, they are called formal parameters. The formal parameters are automatically destroyed when the function call is completed. Therefore, formal parameters are only valid in functions.

Formal parameters also have their own addresses:

int Max(int m, int n)
{
    
    
	printf("m的地址为%p\n", &m);
	printf("n的地址为%p\n", &n);
	return m > n ? m : n;
}
int main()
{
    
    
	int x = 5;
	int y = 4;
	printf("x的地址为%p\n", &x);
	printf("y的地址为%p\n", &y);
	printf("%d\n", Max(y, x));
	return 0;
}

The result is:

the process of formal parameter instantiation must allocate a memory unit, and there will be a corresponding address.

Function call:

  1. Call by value: The formal parameters and actual parameters of the function occupy different memory blocks, and the modification of the formal parameters will not affect the actual parameters.
  2. Call by address: Call by address is a way to call a function by passing the memory address of the variable created outside the function to the function parameter. This way of passing parameters can establish a real connection between the function and the variables outside the function, that is, the variables outside the function can be manipulated inside the function.

Passing address: such a code

int Swap(int *m, int *n)
{
    
    
	int t = *m;
	*m = *n;
	*n = t;
}
int main()
{
    
    
	int x = 5;
	int y = 4;
	Swap(&x, &y);
	return 0;
}

Insert picture description here
Operations are performed on formal parameters, which affect the value of actual parameters.

 
 
 
When an array is used as a function parameter, if an array is passed in, the dimensionality reduction problem of the array will occur when the parameter is actually passed.
Dimensionality reduction problem: Dimensionality reduction into corresponding pointers.
Why do we need to reduce dimensionality?
Improve efficiency by reducing copy costs.

 
 
 
Nested calls and chained access of functions:

  1. Nested call: When calling a function, continue to call the function.
  2. Chained access: Use the return value of one function as a parameter of another function.
int main()
{
    
    
	printf("%d ", printf("%d ", printf("%d ", 43)));
	return 0;
}

What is the result of chained access?

The printf function has a return value, and the return value is the length of the string.

The result is:
Insert picture description here
declaration and definition of the function:

  1. Function declaration:
    · tell the compiler what a function is called, what the parameters are, what the return type yes. But it doesn't matter whether it exists or not.
    · The declaration of a function generally appears before the use of the function, and must be defined before use .
    · The declaration of the function is generally placed in the header file.
  2. Definition of function: The definition of a function refers to the concrete realization of the function, explaining the function realization of the function.

Function recursion:

What is recursion?
The programming technique for a program to call itself is recursion. A procedure or function calls itself directly or indirectly a method, it is usually put in a definition or description thereof large complex problem into a problem similar to the original problem to solve smaller , only a small amount recursion The program can describe the repeated calculations required in the process of solving the problem, which greatly reduces the amount of code of the program. The main way of thinking about recursion is to make big things smaller.

Two necessary conditions for recursion:

  1. There are restrictions, when the restrictions are met, the recursion will not continue. (Recursive exit)
  2. Each recursion gets closer and closer to this restriction.

In the c program address space, the allocated addresses of functions and variables are in the stack area.
Stack area: first-in-last-out, downward growth.
The space allocated by a function on the stack is also called a stack frame.
The life cycle of local variables follows the function (code block).

In some recursive calls, there are a lot of repeated calculations. The stack frame of the function call will cause the stack to overflow. The stack space allocated to the program by the system is limited, but if there is an infinite loop or dead recursion, this may cause Always open up the stack space, eventually the stack space is exhausted, this phenomenon is called stack overflow.

How to solve it?
· Change recursion to non-recursion
· Use static objects instead of nonstatic local objects. It can reduce the cost of generating and releasing nonstatic objects during each recursive call and return, and static objects can also be saved to the intermediate state of recursive calls, and can be accessed by each call layer.

Guess you like

Origin blog.csdn.net/w903414/article/details/105583917