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 namesint add(int a, int b) { int sum = a + b; return sum; }
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; }
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.
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!