Home>Article>Backend Development> What are the basic knowledge of C language functions?

What are the basic knowledge of C language functions?

coldplay.xixi
coldplay.xixi Original
2020-06-11 16:36:58 4456browse


What are the basic knowledge of C language functions?

What are the basic knowledge of C language functions?

Basic knowledge of C language functions include:

1. Definition, declaration and calling

Function: A piece of code that can be reused
Three elements of a function: Return value function name Function parameter list
Function declaration: Contains the three elements of the function
Function definition: Specific implementation of function
Return value: If a function has no return value, it is written as void
Function parameter list: If there are no parameters, write it as empty, and separate the variables with commas

2. Function declaration:

When declaring a function, the formal parameter can be without a variable name, or only with a type.

int add(int, int);


##三, Function definition:

When defining a function, the formal parameters must have variable names

int add(int a, int b) { int sum = a + b; return sum; }


## 4. Function call:

If the function is defined above the main function, there is no need to write a function declaration.


If the function is defined below the main function, the declaration must be written above the main function

int main() { int a = 10; int b = 20; // 函数调用的时候,传的参数叫实参 int sum= add(a, b); printf (“sum = %d\n”, sum); return 0; }


## 5. Function name

The function name is the entry address of the function

Define a function pointer:

int (*p)(int a.int b) =add;

6. Recursive function

A function that calls itself within its function body is called a recursive call. This function is called a recursive function. Executing a recursive function will call itself repeatedly, entering a new level each time.

Use recursion to calculate n!. The calculation formula of factorial n! is as follows:

Programming according to the formula:

long factorial(int n) { long result; if(n ==0 || n ==1) { result = 1; } else { result = factorial(n-1) * n; // 递归调用 } return result; }

This is a typical recursive function. After calling factorial, the function body is entered. The function will end execution only when n== 0 or n==1, otherwise it will keep calling itself.

Since the actual parameter of each call is n-1, that is, the value of n-1 is assigned to the formal parameter n, so the value of the recursive actual parameter is reduced by 1 each time until the last n- When the value of 1 is 1, make a recursive call again, and the value of the formal parameter n is also 1. The recursion will terminate and exit layer by layer.

For example, to find 5!, call factorial(5). When entering the factorial function body, since n=5 is not equal to 0 or 1, result = factorial(n-1) * n; is executed, that is, result = factorial(5-1) * 5;, and the next step is to call factorial(4). This is the first recursion.

After four recursive calls, the value of the actual parameter is 1, that is, factorial(1) is called. At this point the recursion ends and begins to return layer by layer. The value of factorial(1) is 1, the value of factorial(2) is 12=2, the value of factorial(3) is 23=6, the value of factorial(4) is 64=24, and the final return value of factorial(5) is 245=120.

Note:

1. In order to prevent the recursive call from proceeding without termination, there must be a means to terminate the recursive call within the function. A common method is to add conditional judgment. When a certain condition is met, the recursive call will no longer be made and then return layer by layer.

2. Recursive calls are not only difficult to understand, but also very expensive. It is not recommended to use recursion unless necessary. Many recursive calls can be replaced by iterations (loops).

Recommended tutorial: "

C Video Tutorial

"

The above is the detailed content of What are the basic knowledge of C language functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn