C language system learning 3 functions

C language function

1. What is a function
We will come into contact with the concept of function in mathematics, but there is also the term "function" in C language, but the meaning expressed by function in mathematics is completely different. Explained in plain language, a function is a "subroutine"

Subroutine: It is a certain part of code in a large program, consisting of one or more statement blocks. It is responsible for completing a specific task, and compared with other codes, it is relatively independent

For a small requirement or function that we need to implement, we can call it in the form of a function, which greatly improves the readability and simplicity of the code.

Classification of functions in C language:
1. Library functions
have many very commonly used functions. In order to improve portability and improve the efficiency of functions, a series of similar library functions are provided in the basic library of C language, which is convenient for programmers to implement software. develop.
The official description of the library function is provided. If we want to know about the function or use of a certain library function, we can check it through this website
URL: www.cplusplus.com
ps: It is an English version of the document

2. Custom functions
Of course, library functions cannot provide all the functions we want to implement in the code. At this time, we need to define functions according to our needs for use.

A custom function is the same as a library function, with a function name, return value type, and function parameters. We need to define ourselves.
The composition of the function

ret_type fun_name(para1,*)
{
	statement;//语句
}
ret_type  返回类型
fun_name  函数名
para1     参数名

2. Function parameters
Actual parameters (actual parameters):

The actual parameters passed to the function are called actual parameters. Actual parameters can be: constants, variables, expressions, functions, etc. No matter what type of quantity the actual parameters are, they must have definite values ​​when the function is called, so that these values ​​can be transferred to the formal parameters.

Formal parameters (parameters)

Formal parameters refer to the variables in parentheses after the function name, because formal parameters are only instantiated (allocated memory units) during the function call, so they are called formal parameters. Formal parameters are automatically destroyed when the function is called. Therefore formal parameters are only valid within the function.

3. Function call Pass-by
-value call

The formal parameters and actual parameters of the function occupy different memory blocks, and the formal parameters of the function will not affect the actual parameters.

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 inside of the function can directly manipulate the variables outside the function.

Fourth, function declaration and definition
Function declaration:
1. Tell the compiler the name, parameters, and return type of the function.
2. The function must be declared first and then used.
3. The declaration of the function should generally be placed in the header file.
Function definition:
refers to the specific implementation of the function, explaining the function realization of the function

Example: Recursive calculation of power function

int calcifang(int n,int k)
{
	if (k == 1)
		return n;
	else
		return n * calcifang(n, k - 1);
}

Guess you like

Origin blog.csdn.net/qq_45742383/article/details/113664637